Skip to content
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

fix: models copied via Config.copy_on_model_validation always have all fields #3201

Merged
merged 1 commit into from Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 24 additions & 17 deletions pydantic/main.py
Expand Up @@ -580,6 +580,24 @@ def construct(cls: Type['Model'], _fields_set: Optional['SetStr'] = None, **valu
m._init_private_attributes()
return m

def _copy_and_set_values(self: 'Model', values: 'DictStrAny', fields_set: 'SetStr', *, deep: bool) -> 'Model':
if deep:
# chances of having empty dict here are quite low for using smart_deepcopy
values = deepcopy(values)

cls = self.__class__
m = cls.__new__(cls)
object_setattr(m, '__dict__', values)
object_setattr(m, '__fields_set__', fields_set)
for name in self.__private_attributes__:
value = getattr(self, name, Undefined)
if value is not Undefined:
if deep:
value = deepcopy(value)
object_setattr(m, name, value)

return m

def copy(
self: 'Model',
*,
Expand All @@ -599,32 +617,18 @@ def copy(
:return: new model instance
"""

v = dict(
values = dict(
self._iter(to_dict=False, by_alias=False, include=include, exclude=exclude, exclude_unset=False),
**(update or {}),
)

if deep:
# chances of having empty dict here are quite low for using smart_deepcopy
v = deepcopy(v)

cls = self.__class__
m = cls.__new__(cls)
object_setattr(m, '__dict__', v)
# new `__fields_set__` can have unset optional fields with a set value in `update` kwarg
if update:
fields_set = self.__fields_set__ | update.keys()
else:
fields_set = set(self.__fields_set__)
object_setattr(m, '__fields_set__', fields_set)
for name in self.__private_attributes__:
value = getattr(self, name, Undefined)
if value is not Undefined:
if deep:
value = deepcopy(value)
object_setattr(m, name, value)

return m
return self._copy_and_set_values(values, fields_set, deep=deep)

@classmethod
def schema(cls, by_alias: bool = True, ref_template: str = default_ref_template) -> 'DictStrAny':
Expand Down Expand Up @@ -652,7 +656,10 @@ def __get_validators__(cls) -> 'CallableGenerator':
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
if isinstance(value, cls):
return value.copy() if cls.__config__.copy_on_model_validation else value
if cls.__config__.copy_on_model_validation:
return value._copy_and_set_values(value.__dict__, value.__fields_set__, deep=False)
else:
return value

value = cls._enforce_dict_if_root(value)

Expand Down
41 changes: 41 additions & 0 deletions tests/test_main.py
Expand Up @@ -16,7 +16,9 @@
Field,
NoneBytes,
NoneStr,
PrivateAttr,
Required,
SecretStr,
ValidationError,
constr,
root_validator,
Expand Down Expand Up @@ -1504,6 +1506,45 @@ class Config:
assert Model.__fields__['b'].field_info.exclude == {'foo': ..., 'bar': ...}


def test_model_exclude_copy_on_model_validation():
"""When `Config.copy_on_model_validation` is set, it should keep private attributes and excluded fields"""

class User(BaseModel):
_priv: int = PrivateAttr()
id: int
username: str
password: SecretStr = Field(exclude=True)
hobbies: List[str]

my_user = User(id=42, username='JohnDoe', password='hashedpassword', hobbies=['scuba diving'])

my_user._priv = 13
assert my_user.id == 42
assert my_user.password.get_secret_value() == 'hashedpassword'
assert my_user.dict() == {'id': 42, 'username': 'JohnDoe', 'hobbies': ['scuba diving']}

class Transaction(BaseModel):
id: str
user: User = Field(..., exclude={'username'})
value: int

class Config:
fields = {'value': {'exclude': True}}

t = Transaction(
id='1234567890',
user=my_user,
value=9876543210,
)

assert t.user is not my_user
assert t.user.hobbies == ['scuba diving']
assert t.user.hobbies is my_user.hobbies # `Config.copy_on_model_validation` only does a shallow copy
assert t.user._priv == 13
assert t.user.password.get_secret_value() == 'hashedpassword'
assert t.dict() == {'id': '1234567890', 'user': {'id': 42, 'hobbies': ['scuba diving']}}


@pytest.mark.parametrize(
'kinds',
[
Expand Down