[python] reserve generated helper and pydantic model_* property names#24358
Open
fa0311 wants to merge 2 commits into
Open
[python] reserve generated helper and pydantic model_* property names#24358fa0311 wants to merge 2 commits into
fa0311 wants to merge 2 commits into
Conversation
A property named from_dict (or to_dict, to_json, ...) shadows the helper classmethods generated on every model, so Model.from_dict() raises AttributeError. A property named model_config is even worse: the class level 'model_config = ConfigDict(...)' assignment clobbers the field and the value is silently dropped from serialization. model_dump / model_fields etc. shadow the pydantic methods the generated code itself calls. Treat those names as reserved words so they get the usual var_ prefix and keep their wire name through the alias, the same way schema/json/field are handled today. Names like model_configuration that merely share the prefix are not affected. testUnmappedMemberCollisionsRemainOutsideNameMappingValidation pinned the old broken output for unmapped collisions; it now expects the mangled fields (still no hard failure, which is what the test is about).
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.
Property names that collide with the generated helpers or with pydantic's
model_*namespace are not currently escaped, and they break the generated model in a couple of ways.A property named
from_dict(orto_dict,to_json,to_str,from_json) shadows the classmethod generated on every model:A property named
model_configis worse — the class-levelmodel_config = ConfigDict(...)assignment clobbers the field, so the value is silently accepted and then dropped from serialization.model_dump,model_fields,model_validateetc. shadow the pydantic methods the generated code itself calls.These are all real-world-plausible field names (e.g. ML APIs with a
model_configfield), so this is a silent data loss / crash rather than a theoretical concern.The fix treats those names as reserved words, reusing the sets the generator already maintains (
GENERATED_MODEL_MEMBER_NAMESand themodel_*entries ofPYDANTIC_BASE_MODEL_MEMBER_NAMES). They get the usualvar_prefix and keep their wire name through the alias, exactly likeschema/json/fieldtoday:Names that merely share the prefix, like
model_configuration, are untouched.Added a unit test for the mangling, and updated
testUnmappedMemberCollisionsRemainOutsideNameMappingValidation, which pinned the old (broken) unmapped output — it now expects the mangled fields.PythonClientCodegenTestpasses and the python samples are unchanged.PR checklist
./mvnw clean packageand regenerated the python samples (no diff).master./cc @cbornet @tomplus @krjakbrjak
Summary by cubic
Reserve Python model property names that collide with generated helpers and
pydanticmodel_*methods to prevent crashes and silent data loss. Fields liketo_dictandmodel_confignow mangle tovar_*while keeping their wire names via aliases.from_dict,to_dict,to_json,to_str,from_json) andpydanticmodel_*methods (model_config,model_dump,model_fields, etc.).var_<name>withalias="<wire_name>"; similar names (e.g.,model_configuration) are not affected.Written for commit c6f1d00. Summary will update on new commits.