Skip to content

Commit

Permalink
Fix master (#1682)
Browse files Browse the repository at this point in the history
* Fix master

* 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

* conset invalid defualt test

* add change

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
  • Loading branch information
samuelcolvin and PrettyWood committed Jul 9, 2020
1 parent dac6764 commit a913c37
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions changes/1682-samuelcolvin.md
@@ -0,0 +1 @@
Modify validators for `conlist` and `conset` to not have `always=True`.
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
30 changes: 24 additions & 6 deletions tests/test_types.py
Expand Up @@ -239,20 +239,38 @@ 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):
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):
v: conset(int) = {}
class Model(BaseModel):
v: conset(int) = set()

m = Model()
assert m.v == set()


def test_constrained_set_default_invalid():
class Model(BaseModel):
v: conset(int) = 'not valid, not validated'

m = ConSetModelMax()
assert m.v == {}
m = Model()
assert m.v == 'not valid, not validated'


def test_constrained_set_too_long():
Expand Down

0 comments on commit a913c37

Please sign in to comment.