[Bug] Python codegen overwrites keyword-suffixed fields
Search before asking
- [x] Searched existing issues and PRs for keyword/escape/suffix collisions. Related merged #4107 handles helper-name suffix families; it does not handle this keyword case. No matching active fix found in the bounded search.
Version
Apache Fory main 8b27a2d54c74b577a05155e0d94691dcf8007fa9, Windows amd64, CPython 3.11.15. Component: compiler / Python code generation.
Minimal reproduce step
Save as keyword_collision.fdl:
package keyword_collision;
message Record [id=100] {
string class = 1;
string class_ = 2;
}
From the compiler directory:
python -m fory_compiler keyword_collision.fdl --lang python --output generated
The schema validates, generation exits successfully, and the emitted class has two declarations for the same Python attribute:
class Record:
class_: str = pyfory.field(id=1, default="")
class_: str = pyfory.field(id=2, default="")
What did you expect to see?
Both distinct schema fields and their numeric tag IDs remain represented in the generated dataclass.
What did you see instead?
Executing the generated class with the repository's unchanged python/pyfory/field.py dataclass/field implementation produces only {'class_': 2}. The earlier declaration/tag 1 is overwritten. A control schema using class_other as the second name preserves both tags.
This probe directly checks class construction; it does not claim a native serialization/deserialization roundtrip.
Contributor explanation
Translated from my own explanation: this is an edge-case correctness bug. Missing handling for attribute names causes definitions to overwrite each other, rather than preserving what the schema expresses.
Anything else?
PythonGenerator.safe_field_name already shifts the entire suffix family for helper names such as field, field_, and field__, but keywords fall through to a single-name escape. A focused fix extends the same suffix-family rule to keyword names, with generated-class regression coverage and the owning documentation updated.
Generated Python attribute names change on regeneration: class -> class_, class_ -> class__, and class__ -> class___. The suffix rule applies even when the unsuffixed keyword is absent. Call sites using those generated attributes may need to change; numeric tag IDs stay unchanged. This is a code-generation correctness report, not a runtime security claim or a proposed wire-format change.
AI assistance
AI assisted discovery, reproduction, the candidate patch, and test execution. The contributor reviewed the local diff, explained the defect, and personally ran the focused Python generator tests after the fix: 41 passed in 0.26s. Broader AI-run verification and final review evidence will be disclosed in the linked PR, separately from human verification.
Are you willing to submit a PR?
- [x] I'm willing to submit a PR.
Final AI review artifacts
Both reviews below were started in new clean contexts after the contributor completed personal code self-review. They reviewed the exact final commit 3ab8274ab03015ee28b21aa3f181452fd70a54c5 against parent 8b27a2d54c74b577a05155e0d94691dcf8007fa9: 3 files, +26/-9. No code changes followed the reviews. This is persisted AI review evidence, not a claim that AI reviewers performed human verification.
Reviewer A — Fory-guided fresh review
Workflow identity: fory_guided_post_human, new read-only review subagent. Final output:
Reviewed commit
3ab8274ab03015ee28b21aa3f181452fd70a54c5against its parent and fetchedapache/main,8b27a2d54c74b577a05155e0d94691dcf8007fa9: 3 files, +26/-9.No further actionable comments.
Followed
AGENTS.md,.agents/ci-and-pr.md, and applicable Python/documentation guidance. Independently inspected the patch and relevant generator/runtime ownership paths without consulting previous review results.The naming change preserves distinct keyword-family fields, their tag IDs, and consistent generated representations. Regression tests exercise actual schema parsing and generated classes across both declaration orders and normal/custom
reprpaths. Documentation correctly states that suffixed names receive another underscore even when the unsuffixed name is absent.Scope remains one logical compiler fix; no review-driven expansion or unresolved findings.
Validation limits: inspected existing logs showing RED 20 failed / 21 passed, focused GREEN 41 passed, and full compiler 551 passed / 5 failed. The supplied baseline comparison identifies the five failures as existing missing-.NET/Windows Dart-path issues. The real
field.pyconstruction check preserves IDs and values but does not establish native serialization round-trip coverage.This review was read-only: no edits, tests, builds, linters, benchmarks, or publication were performed.
Reviewer B — Independent general reviewer
Workflow identity: fresh Codex structured-review session 01a1337d-8e8f-74a2-abf9-a9d529ccafdf, label Independent general reviewer, post-human final review. This separate clean-context reviewer followed the independent-review carve-out and was not pointed to .agents/ci-and-pr.md or a copied Fory-specific review checklist. It did not consult Reviewer A's output. Helper exit: 0.
Final structured output:
{
"findings": [],
"overall_correctness": "patch is correct",
"overall_explanation": "Reviewed commit 3ab8274ab03015ee28b21aa3f181452fd70a54c5 against parent 8b27a2d54c74b577a05155e0d94691dcf8007fa9. The change consistently shifts keyword suffix families, preserves distinct generated attributes and numeric tags, and uses matching names in generated repr methods. Tests and documentation align with this behavior. No further actionable comments. No tests were run during this read-only review; the contributor reported 41 passing focused tests.",
"overall_confidence": 0.98
}
Verification recorded separately from reviews
Contributor personally ran, on the unchanged patch, from compiler/ with ENABLE_FORY_DEBUG_OUTPUT=1:
python -m pytest fory_compiler/tests/test_python_generator.py -q
......................................... [100%]
41 passed in 0.26s
Additional AI-run checks: original-code RED 20 failed/21 passed; fixed-code focused GREEN41 passed; complete compiler551 passed/same5 baseline failures (missing .NET SDK and four Windows Dart-path failures). Ruff format/check, Prettier, and git diff --check passed. The actual-owner CLI probe demonstrates class construction and retained fields, not a native binary roundtrip. The PR discloses the full-suite limitations rather than claiming all checks passed.