New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model with field name that shadows existing BaseModel attribute leads to unexpected behavior #242
Comments
Probably best to raise an exception? |
I thought of it but it would have been painful in my case. I am parsing an OpenAPI 3.0 document(https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject) so I do not control the format. Not being able to use pydantic for this would be annoying. My current workaround is to expose the schema field as |
You can use aliases so the external property Mgmt can be different. |
FYI I had a similar problem with an OpenAPI field called |
Oh thanks, let me look into aliases. If that works then an exception seems fine. |
Yeah alisases work well. Thanks! In this case an exception would be fine (with perhaps a hint to use an alias). |
Would you like to submit a pr? |
I will try... |
|
I think it will confuse people a lot of methods and/or attributes change between the class and the instance. Eg. Better to stick to classmethods and raise an exception if fields are setup with any of their names. |
I am a bit surprised this PR made it into pydantic. Think about building an ORM. Imagine all of your pydantic models (like Users in the example below) extend your own base
All fine, until the user wants a field called With the above PR |
Perhaps allow shadowing could be a config variable? That would be helpful. |
See #1001. |
@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 |
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 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 |
* WIP, default validator * fixing tests * use DefaultValidator in arguments * simplifying schema for type-dict defaults * support omit on lists * improve coverage * add benchmark * fix tests * test compatibility with pytest-benchmark * fix tests * supporting tuples * small tweaks * more tests and tweak codecov.yml * fix tests * support for dict omit
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:
This prints
<bound method BaseModel.schema of <class 'pydantic.main.BaseModel'>>
instead ofabc
.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?
The text was updated successfully, but these errors were encountered: