fix(compiler): escape Python field names that shadow generated default helpers - #4107
Open
ayush00git wants to merge 2 commits into
Open
ayush00git wants to merge 2 commits into
ayush00git wants to merge 2 commits into
Conversation
…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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
A schema such as
message Tricky { string pyfory = 1; list<string> tags = 2; }generates a dataclass whosepyforyattribute shadows the module-levelpyforyimport, so later field defaults likepyfory.field(id=2, ...)raiseAttributeErrorwhen the generated module is imported. The same shadowing happens for fields namedfield,decimal,list, ordict, 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_casestrips 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).