Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix master #1682

Merged
merged 4 commits into from Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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