Skip to content

Commit

Permalink
Fix master 2 (#1694)
Browse files Browse the repository at this point in the history
* test: add regression test with wrong type default

* fix: remove always on conlist and conset

* fix: use utf8 as default encoding on all OS
  • Loading branch information
PrettyWood committed Jul 9, 2020
1 parent 66a4d3b commit 4658cf8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pydantic/env_settings.py
Expand Up @@ -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:
Expand Down
10 changes: 2 additions & 8 deletions pydantic/fields.py
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_types.py
Expand Up @@ -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) = []
Expand Down

0 comments on commit 4658cf8

Please sign in to comment.