You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into an issue while using a property with a pydantic class which led to an unexpected error.
To reproduce this error, you can simply create a pydantic class, define a property in it, and raise an AttributeError inside the property function. Instead of displaying this error, the stack trace will say:
AttributeError: 'MyModel' object has no attribute 'my_property'
Important to note, that this is the case only if extra is set to "allow" in the corresponding ConfigDict. If this line is disabled, the code raises the right error.
While it seems simple to see what the root cause of the failure is here in the example code, in complicated cases (where the call on the property executes some logic that ultimately leads to an AttributeError), this can be problematic and confusing as this does not showcase the correct error.
Example Code
frompydanticimportBaseModel, ConfigDictclassMyModel(BaseModel):
@propertydefmy_property(self):
"""A random property."""raiseAttributeError("Original Error!")
model_config=ConfigDict(extra="allow")
my_model=MyModel()
my_model.my_property
Huh, that's odd! Thanks for reporting. Definitely a bug. Happens when you have the computed_field decorator over said property as well, which is even more concerning.
try:
pydantic_extra=object.__getattribute__(self, '__pydantic_extra__')
exceptAttributeError:
pydantic_extra=Noneifpydantic_extraisnotNone:
try:
returnpydantic_extra[item]
exceptKeyErrorasexc:
raiseAttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') fromexc# ...# Regular attribute handling continues here
When setting extra=True in model_config, __pydantic_extra__ is defined as an empty dictionary. That means that execution will enter this if block and try to get the attribute from the __pydantic_extra__ dict, which would be wrong.
I changed the if statement to be
Initial Checks
Description
Hello,
I ran into an issue while using a property with a pydantic class which led to an unexpected error.
To reproduce this error, you can simply create a pydantic class, define a
property
in it, and raise anAttributeError
inside theproperty
function. Instead of displaying this error, the stack trace will say:AttributeError: 'MyModel' object has no attribute 'my_property'
Important to note, that this is the case only if
extra
is set to"allow"
in the correspondingConfigDict
. If this line is disabled, the code raises the right error.While it seems simple to see what the root cause of the failure is here in the example code, in complicated cases (where the call on the property executes some logic that ultimately leads to an
AttributeError
), this can be problematic and confusing as this does not showcase the correct error.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: