question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model with field name that shadows existing BaseModel attribute leads to unexpected behavior

See original GitHub issue

Hello, 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:closed
  • Created 5 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
laydaycommented, Sep 9, 2020

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:

from pydantic.main import ModelMetaclass, BaseModel


class UsersMeta(ModelMetaclass):
    def find(cls, pk: int) -> 'Users': ...


class Users(BaseModel, metaclass=UsersMeta):
    find: int

Users.find  # Should this be `int` or `(pk: int) -> Users`?

Anyway, this will work without requiring any changes to Pydantic, assuming that you don’t step on any of its own methods.

1reaction
mreschkecommented, Sep 9, 2020

@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 in config: Type['BaseConfig'] and make it a config property so we can control the shadow behavior from a config that would be a flexible solution.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found