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 have searched GitHub for a duplicate issue and I'm sure this is something new
I have searched Google & StackOverflow for a solution and couldn't find anything
I have read and followed the docs and still think this is a bug
I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)
Description
When a model has a field with a dict "subclass" (eg: defaultdict, Box, etc), you can normally create and .copy() the model retaining the dict subclass (implemented in #2325 and #3138 - not sure of the functional difference between them 🤔).
However, if you pass an include/exclude to .copy() or have a Field(..., exclude=True), then the call to BaseModel._get_value() will convert the subclass to a direct dict:
if (notvalue_excludeornotvalue_exclude.is_excluded(k_))
and (notvalue_includeorvalue_include.is_included(k_))
}
This results in random issues depending on the subclass and usage (in my example, I was using a frozen Model+Box to preserve hash-ability and all sorts of other things).
Humm, I doubt we can fix this in v1, I'm not even sure how we can fix this in V2, I think we should only allow include/exclude on known types - models, dataclasses, dict, list, tuple - in particular I there think we will need an error if you try to include/exclude on a type we can't help on.
We’re no longer actively developing Pydantic V1 (although it will continue to receive security fixes for the next year or so), so we’re closing issues solely related to V1. If you really think this shouldn’t be closed, please comment or create a new issue 🚀.
Initial Checks
Description
When a model has a field with a
dict
"subclass" (eg:defaultdict
,Box
, etc), you can normally create and.copy()
the model retaining the dict subclass (implemented in #2325 and #3138 - not sure of the functional difference between them 🤔).However, if you pass an include/exclude to
.copy()
or have aField(..., exclude=True)
, then the call toBaseModel._get_value()
will convert the subclass to a direct dict:pydantic/pydantic/main.py
Lines 485 to 500 in 38f0b4e
This results in random issues depending on the subclass and usage (in my example, I was using a frozen Model+Box to preserve hash-ability and all sorts of other things).
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: