Skip to content

Commit 51f1ba4

Browse files
authored
Add version number to Python code comments (#1511)
* feat: Add sqlc version to generated Python code * feat: Add source file to Python code comments
1 parent 582ba6e commit 51f1ba4

File tree

14 files changed

+60
-20
lines changed

14 files changed

+60
-20
lines changed

examples/python/src/authors/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35
from typing import Optional
46

examples/python/src/authors/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: query.sql
25
from typing import AsyncIterator, Iterator, Optional
36

47
import sqlalchemy

examples/python/src/booktest/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35
import datetime
46
import enum

examples/python/src/booktest/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: query.sql
25
import dataclasses
36
import datetime
47
from typing import AsyncIterator, List, Optional

examples/python/src/jets/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35

46

examples/python/src/jets/query-building.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: query-building.sql
25
from typing import AsyncIterator, Optional
36

47
import sqlalchemy

examples/python/src/ondeck/city.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: city.sql
25
from typing import AsyncIterator, Optional
36

47
import sqlalchemy

examples/python/src/ondeck/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35
import datetime
46
import enum

examples/python/src/ondeck/venue.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: venue.sql
25
import dataclasses
36
from typing import AsyncIterator, List, Optional
47

internal/codegen/python/gen.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
1212
"github.com/kyleconroy/sqlc/internal/inflection"
13+
"github.com/kyleconroy/sqlc/internal/info"
1314
"github.com/kyleconroy/sqlc/internal/metadata"
1415
"github.com/kyleconroy/sqlc/internal/plugin"
1516
pyast "github.com/kyleconroy/sqlc/internal/python/ast"
@@ -481,6 +482,30 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
481482
return qs, nil
482483
}
483484

485+
func moduleNode(source string) *pyast.Module {
486+
mod := &pyast.Module{
487+
Body: []*pyast.Node{
488+
poet.Comment(
489+
"Code generated by sqlc. DO NOT EDIT.",
490+
),
491+
poet.Comment(
492+
"versions:",
493+
),
494+
poet.Comment(
495+
" sqlc " + info.Version,
496+
),
497+
},
498+
}
499+
if source != "" {
500+
mod.Body = append(mod.Body,
501+
poet.Comment(
502+
"source: "+source,
503+
),
504+
)
505+
}
506+
return mod
507+
}
508+
484509
func importNode(name string) *pyast.Node {
485510
return &pyast.Node{
486511
Node: &pyast.Node_Import{
@@ -636,18 +661,7 @@ func buildImportGroup(specs map[string]importSpec) *pyast.Node {
636661
}
637662

638663
func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node {
639-
mod := &pyast.Module{
640-
Body: []*pyast.Node{
641-
{
642-
Node: &pyast.Node_Comment{
643-
Comment: &pyast.Comment{
644-
Text: "Code generated by sqlc. DO NOT EDIT.",
645-
},
646-
},
647-
},
648-
},
649-
}
650-
664+
mod := moduleNode("")
651665
std, pkg := i.modelImportSpecs()
652666
mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg))
653667

@@ -779,14 +793,7 @@ func asyncQuerierClassDef() *pyast.ClassDef {
779793
}
780794

781795
func buildQueryTree(ctx *pyTmplCtx, i *importer, source string) *pyast.Node {
782-
mod := &pyast.Module{
783-
Body: []*pyast.Node{
784-
poet.Comment(
785-
"Code generated by sqlc. DO NOT EDIT.",
786-
),
787-
},
788-
}
789-
796+
mod := moduleNode(source)
790797
std, pkg := i.queryImportSpecs(source)
791798
mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg))
792799
mod.Body = append(mod.Body, &pyast.Node{

internal/endtoend/testdata/exec_result/python_postgresql/python/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35

46

internal/endtoend/testdata/exec_result/python_postgresql/python/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: query.sql
25
import sqlalchemy
36
import sqlalchemy.ext.asyncio
47

internal/endtoend/testdata/exec_rows/python_postgresql/python/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
24
import dataclasses
35

46

internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.12.0
4+
# source: query.sql
25
import sqlalchemy
36
import sqlalchemy.ext.asyncio
47

0 commit comments

Comments
 (0)