From 66a4d3b013eeb7c0e7cd321840f0febdbc9b7b30 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 4 Jul 2020 11:51:25 +0100 Subject: [PATCH 1/4] Fix master --- tests/test_types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 90a5210374..94f42a6444 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -249,10 +249,10 @@ class ConSetModelMax(BaseModel): def test_constrained_set_default(): class ConSetModelMax(BaseModel): - v: conset(int) = {} + v: conset(int) = set() m = ConSetModelMax() - assert m.v == {} + assert m.v == set() def test_constrained_set_too_long(): From 4658cf88732a0f52c352544bee3dd8575ebc82be Mon Sep 17 00:00:00 2001 From: PrettyWood Date: Thu, 9 Jul 2020 14:29:29 +0200 Subject: [PATCH 2/4] Fix master 2 (#1694) * test: add regression test with wrong type default * fix: remove always on conlist and conset * fix: use utf8 as default encoding on all OS --- pydantic/env_settings.py | 2 +- pydantic/fields.py | 10 ++-------- tests/test_types.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pydantic/env_settings.py b/pydantic/env_settings.py index 05fcf02671..503ff4ed31 100644 --- a/pydantic/env_settings.py +++ b/pydantic/env_settings.py @@ -129,7 +129,7 @@ def read_env_file(file_path: Path, *, encoding: str = None, case_sensitive: bool except ImportError as e: raise ImportError('python-dotenv is not installed, run `pip install pydantic[dotenv]`') from e - file_vars: Dict[str, Optional[str]] = dotenv_values(file_path, encoding=encoding) + file_vars: Dict[str, Optional[str]] = dotenv_values(file_path, encoding=encoding or 'utf8') if not case_sensitive: return {k.lower(): v for k, v in file_vars.items()} else: diff --git a/pydantic/fields.py b/pydantic/fields.py index f93a9d9b32..89c70da5b2 100644 --- a/pydantic/fields.py +++ b/pydantic/fields.py @@ -451,10 +451,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity) get_validators = getattr(self.type_, '__get_validators__', None) if get_validators: self.class_validators.update( - { - f'list_{i}': Validator(validator, pre=True, always=True) - for i, validator in enumerate(get_validators()) - } + {f'list_{i}': Validator(validator, pre=True) for i, validator in enumerate(get_validators())} ) self.type_ = self.type_.__args__[0] @@ -464,10 +461,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity) get_validators = getattr(self.type_, '__get_validators__', None) if get_validators: self.class_validators.update( - { - f'set_{i}': Validator(validator, pre=True, always=True) - for i, validator in enumerate(get_validators()) - } + {f'set_{i}': Validator(validator, pre=True) for i, validator in enumerate(get_validators())} ) self.type_ = self.type_.__args__[0] diff --git a/tests/test_types.py b/tests/test_types.py index 94f42a6444..f2f69dc236 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -239,6 +239,16 @@ class Model(BaseModel): assert exc_info.value.errors() == [{'loc': ('foo',), 'msg': 'value is not a valid list', 'type': 'type_error.list'}] +def test_conlist_wrong_type_default(): + """It should not validate default value by default""" + + class Model(BaseModel): + v: conlist(int) = 'a' + + m = Model() + assert m.v == 'a' + + def test_constrained_set_good(): class ConSetModelMax(BaseModel): v: conset(int) = [] From de861ab4d4827b65725ca043fb96e4eeedcea231 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 9 Jul 2020 13:33:36 +0100 Subject: [PATCH 3/4] conset invalid defualt test --- tests/test_types.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index f2f69dc236..125d8fa54b 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -250,21 +250,29 @@ class Model(BaseModel): def test_constrained_set_good(): - class ConSetModelMax(BaseModel): + class Model(BaseModel): v: conset(int) = [] - m = ConSetModelMax(v=[1, 2, 3]) + m = Model(v=[1, 2, 3]) assert m.v == {1, 2, 3} def test_constrained_set_default(): - class ConSetModelMax(BaseModel): + class Model(BaseModel): v: conset(int) = set() - m = ConSetModelMax() + m = Model() assert m.v == set() +def test_constrained_set_default_invalid(): + class Model(BaseModel): + v: conset(int) = 'not valid, not validated' + + m = Model() + assert m.v == 'not valid, not validated' + + def test_constrained_set_too_long(): class ConSetModelMax(BaseModel): v: conset(int, max_items=10) = [] From f035da35a1e542fa2dd471ee44bdbf4862216936 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 9 Jul 2020 14:20:36 +0100 Subject: [PATCH 4/4] add change --- changes/1682-samuelcolvin.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/1682-samuelcolvin.md diff --git a/changes/1682-samuelcolvin.md b/changes/1682-samuelcolvin.md new file mode 100644 index 0000000000..6482eeba17 --- /dev/null +++ b/changes/1682-samuelcolvin.md @@ -0,0 +1 @@ +Modify validators for `conlist` and `conset` to not have `always=True`.