From 4658cf88732a0f52c352544bee3dd8575ebc82be Mon Sep 17 00:00:00 2001 From: PrettyWood Date: Thu, 9 Jul 2020 14:29:29 +0200 Subject: [PATCH] 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) = []