Skip to content

fix(compiler): escape Python field names that shadow generated default helpers - #4107

Open
ayush00git wants to merge 2 commits into
apache:mainfrom
ayush00git:fix/python-generator-field-shadowing
Open

ayush00git wants to merge 2 commits into
apache:mainfrom
ayush00git:fix/python-generator-field-shadowing

Conversation

@ayush00git

Copy link
Copy Markdown
Contributor

What does this PR do?

A schema such as message Tricky { string pyfory = 1; list<string> tags = 2; } generates a dataclass whose pyfory attribute shadows the module-level pyfory import, so later field defaults like pyfory.field(id=2, ...) raise AttributeError when the generated module is imported. The same shadowing happens for fields named field, decimal, list, or dict, which are all referenced by generated default expressions. This PR escapes those field names with a trailing underscore, reusing the existing keyword-escaping convention. The wire field name is unchanged because pyfory's _to_snake_case strips the trailing underscore when deriving the serialized field name.

Verification: the new regression test fails without the generator change and passes with it; the full compiler suite (python3 -m pytest fory_compiler/tests) passes (492 passed, 1 skipped).

…t helpers

A schema field named pyfory, field, decimal, list, or dict becomes a
dataclass attribute that shadows the module-level helper used by every
later field default expression in the same class body, so the generated
module crashes at import time (e.g. AttributeError on pyfory.field).
Escape such field names with a trailing underscore, like keyword
escaping; pyfory strips the trailing underscore when deriving the wire
field name, so the wire format is unchanged.

Claude-Session: https://claude.ai/code/session_01YcPbRyTJYmU8t6mkGCbnUf
@ayush00git
ayush00git requested a review from chaokunyang October 1, 2026 12:26

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant