Model with field name that shadows existing BaseModel attribute leads to unexpected behavior
See original GitHub issueHello, First of all this is an awesome project, I want to use it in all my tools 😃.
I ran into a small issue: when defining a model that has a field with the same name as one of BaseModel’s existing attributes/methods, the field gets parsed but cannot be retrieved:
class BadModel(BaseModel):
schema: str
obj = BaseModel(**{'schema': 'abc'})
print(obj.schema)
This prints <bound method BaseModel.schema of <class 'pydantic.main.BaseModel'>>
instead of abc
.
This makes perfect sense when looking at the code but is a bit unexpected. To be honest, I’m not sure what the “right” behavior should be here?
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (12 by maintainers)
Top Results From Across the Web
Model with field name that shadows existing BaseModel ...
I ran into a small issue: when defining a model that has a field with the same name as one of BaseModel's existing...
Read more >Pydantic field does not take value - Stack Overflow
NameError: Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema'". Following the documentation, I ...
Read more >pydantic/Lobby - Gitter
Seems to fail the codecov bot due to decreased coverage. ... Field name "json" shadows a BaseModel attribute; use a different field name...
Read more >Field name "schema" shadows a BaseModel attribute; use a ...
This error is encountered when you define a pydantic class that has a attributed named schema . The schema attribute is special field...
Read more >pydantic - PyPI
Allow for shallow copies of model fields, Config.copy_on_model_validation is ... an exception if a field's name shadows an existing BaseModel attribute #242 ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, that kind of shadowing isn’t compatible with type checking in Python; you cannot have a field with a disparate type on a sub-model. You can define your class methods on the metaclass so that they are not present on instances of
Users
but your type checker (assuming you use one) won’t know what to do with them either:Anyway, this will work without requiring any changes to
Pydantic
, assuming that you don’t step on any of its own methods.@layday thanks I have seen that interesting thread. But that discussion is more around public instance variables as opposed to class variable vs instance variable shadowing. Obviously you wouldn’t want to remove the
utils.validate_field_name
at this point since its been there since 0.12. But if we can pass inconfig: Type['BaseConfig']
and make it a config property so we can control the shadow behavior from a config that would be a flexible solution.