| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
…n reserved keywords Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
…figDict Signed-off-by: QuentinN42 <quentin@lieumont.fr>
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
There was a problem hiding this comment.
This PR addresses the handling of Python reserved keywords in generated code by transforming field names that conflict with keywords (e.g., class → class_) and adding appropriate Pydantic configuration for serialization/deserialization.
Copilot reviewed 50 out of 52 changed files in this pull request and generated 5 comments.
Show a summary per file| File | Description |
|---|---|
| internal/poet/reserved.go | New file with reserved keyword list and transformation functions |
| internal/poet/reserved_test.go | Tests for reserved keyword detection and transformation |
| internal/poet/builders.go | Modified Name() builder to apply field name transformation |
| internal/gen.go | Updated pydanticNode() to add model_config and fieldNode() to handle reserved keywords |
| internal/gen_test.go | New unit tests for field node generation with reserved keywords |
| internal/endtoend/endtoend_test.go | Modified error message output in test |
| internal/endtoend/testdata/emit_pydantic_models_with_reserved_keywords/* | New end-to-end test for Pydantic with reserved keywords |
| internal/endtoend/testdata/dataclasses_with_reserved_keywords/* | New end-to-end test for dataclasses with reserved keywords |
| internal/endtoend/testdata/emit_pydantic_models/db/models.py | Updated to include model_config in all Pydantic models |
| internal/endtoend/testdata/*/sqlc.yaml | Updated WASM SHA256 checksums |
| internal/endtoend/testdata//python/.py | Updated version comments from v1.28.0 to v1.29.0 |
| examples/src//.py | Updated version comments from v1.28.0 to v1.29.0 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
| Body: poet.Nodes( | ||
| &pyast.Assign{ | ||
| Targets: []*pyast.Node{ | ||
| { | ||
| Node: &pyast.Node_Name{ | ||
| Name: &pyast.Name{Id: "model_config"}, | ||
| }, | ||
| }, | ||
| }, | ||
| Value: poet.Node( | ||
| &pyast.Call{ | ||
| Func: &pyast.Node{ | ||
| Node: &pyast.Node_Attribute{ | ||
| Attribute: &pyast.Attribute{ | ||
| Value: &pyast.Node{ | ||
| Node: &pyast.Node_Name{ | ||
| Name: &pyast.Name{ | ||
| Id: "pydantic", | ||
| }, | ||
| }, | ||
| }, | ||
| Attr: "ConfigDict", | ||
| }, | ||
| }, | ||
| }, | ||
| Keywords: []*pyast.Keyword{ | ||
| { | ||
| Arg: "validate_by_alias", | ||
| Value: poet.Name("True"), | ||
| }, | ||
| { | ||
| Arg: "validate_by_name", | ||
| Value: poet.Name("True"), | ||
| }, | ||
| }, | ||
| }, | ||
| ), | ||
| }, | ||
| ), |
There was a problem hiding this comment.
The model_config with validate_by_alias=True and validate_by_name=True is being added to ALL Pydantic models, even those without reserved keywords. This adds unnecessary validation overhead for models that don't need it. Consider only adding this configuration when the model actually contains fields with reserved keyword names, or at minimum make it conditional based on whether any field in the model uses reserved keywords.
Sorry, something went wrong.
| return &ast.Node{ | ||
| Node: &ast.Node_Name{ | ||
| Name: &ast.Name{Id: id}, | ||
| Name: &ast.Name{Id: FieldName(id)}, |
There was a problem hiding this comment.
Critical bug: Applying FieldName(id) to ALL uses of poet.Name() will break code generation. The Name function is used for many purposes beyond field names, including:
Once the reserved keywords list includes "True", "False", "None", "for", "return", "yield", "is", "not", "in", "or", "and", "as", "with", "if", "else", etc., this will transform all these identifiers incorrectly. This change should be reverted, and FieldName() should only be applied in specific contexts where field names are being generated.
Sorry, something went wrong.
| } | ||
| } | ||
|
|
||
| func Test_FieldName_ShouldUdateReservedKeywords(t *testing.T) { |
There was a problem hiding this comment.
Typo in function name: "ShouldUdateReservedKeywords" should be "ShouldUpdateReservedKeywords"
Sorry, something went wrong.
| var reservedKeywords = []string{ | ||
| "class", | ||
| "if", | ||
| "else", | ||
| "elif", | ||
| "not", | ||
| "for", | ||
| "and", | ||
| "in", | ||
| "is", | ||
| "or", | ||
| "with", | ||
| "as", | ||
| "assert", | ||
| "break", | ||
| "except", | ||
| "finally", | ||
| "try", | ||
| "raise", | ||
| "return", | ||
| "yield", | ||
| } |
There was a problem hiding this comment.
The reserved keywords list is incomplete. Python 3.x has 35 keywords, but only 17 are included here. Missing keywords include: False, None, True, async, await, continue, def, del, from, global, import, lambda, nonlocal, pass, while, and match/case (Python 3.10+). This could cause syntax errors when database columns use these names.
Sorry, something went wrong.
| got, err := cmd.CombinedOutput() | ||
| if diff := cmp.Diff(string(want), string(got)); diff != "" { | ||
| t.Errorf("sqlc diff mismatch (-want +got):\n%s", diff) | ||
| t.Errorf("sqlc diff mismatch (-want +got):\n%s", got) |
There was a problem hiding this comment.
This change breaks the test logic. The error message should show diff (the actual difference between want and got) to help debug test failures, not got (the raw output). This makes it harder to understand what went wrong when tests fail.
| t.Errorf("sqlc diff mismatch (-want +got):\n%s", got) | |
| t.Errorf("sqlc diff mismatch (-want +got):\n%s", diff) |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Fixes #88
To fix this issue, here is my proposition:
Changes overview
Questions / possible issues