Skip to content

Commit

Permalink
better support for allow_mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Mar 3, 2021
1 parent 8846e1b commit 9826145
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pydantic/fields.py
Expand Up @@ -106,7 +106,8 @@ class FieldInfo(Representation):
'extra',
)

__field_constraints__ = { # field constraints with the default value
# field constraints with the default value, it's also used in update_from_config below
__field_constraints__ = {
'min_length': None,
'max_length': None,
'regex': None,
Expand Down Expand Up @@ -164,7 +165,7 @@ def update_from_config(self, from_config: Dict[str, Any]) -> None:
# attr_name is not an attribute of FieldInfo, it should therefore be added to extra
self.extra[attr_name] = value
else:
if current_value is None:
if current_value is self.__field_constraints__.get(attr_name, None):
setattr(self, attr_name, value)

def _validate(self) -> None:
Expand Down
23 changes: 19 additions & 4 deletions tests/test_edge_cases.py
Expand Up @@ -1814,13 +1814,28 @@ class Config:


def test_config_field_info_allow_mutation():
"""
allow_mutation cannot be customised via Config.field because it has a default which is not None
"""
class Foo(BaseModel):
a: str = Field(...)

class Config:
fields = {'a': {'allow_mutation': False}}
validate_assignment = True

assert Foo.__fields__['a'].field_info.allow_mutation is True

f = Foo(a='x')
f.a = 'y'
assert f.dict() == {'a': 'y'}

class Bar(BaseModel):
a: str = Field(...)

class Config:
fields = {'a': {'allow_mutation': False}}
validate_assignment = True

assert Bar.__fields__['a'].field_info.allow_mutation is False

b = Bar(a='x')
with pytest.raises(TypeError):
b.a = 'y'
assert b.dict() == {'a': 'x'}

0 comments on commit 9826145

Please sign in to comment.