-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
validate_assignment=True
and using a stricter __pydantic_extra__
goes wrong.
#10054
Comments
Definitely looks like a bug. Seems like the following code is at play: |
You can however omit the |
Thank you. I'm really not familiar with pydantic's internals so this all seems very strange to me! May be worth pointing out that the error persists if one uses the suggested form of https://docs.pydantic.dev/latest/concepts/models/#extra-fields i.e. class Model(BaseModel):
__pydantic_extra__: Dict[str, int] = Field(init=False)
model_config = ConfigDict(extra="allow",
validate_assignment=True)
m = Model()
m.a = 1
assert m.__pydantic_extra__ == {'a': 1}
# also fails |
Hmm, I'm not so convinced this is a bug. This is the behavior I'd expect - |
@sydney-runkle the issue is regarding |
Ah yes, that's not right... |
I presume we can just add an exclusionary rule for that case |
Just a side note, this is semi related: #9672. We could potentially tackle these at the same time. |
Initial Checks
Description
When defining a model which has a restriction on
__pydantic_extra__
, used in conjunction withvalidate_assignment
, assignment of extra attributes causes strange things to happen to__pydantic_extra__
i.e.__pydantic_extra__
is put inside__pydantic_extra__
.The logic that seems to sort out putting extra values into the right place is here:
https://github.com/aaraney/pydantic/blob/91c2bf19f61c610165de72262f723d6f2b233d28/pydantic/main.py#L737-L742
But it appears that prior to that, because
validating_assignment=True
, we send the new values off to__pydantic_validator__
to to do the validation and setting, where presumably the issue lies.Or maybe I'm getting muddled up and this is all fine.
Many thanks for the useful library.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: