Skip to content

Commit

Permalink
Fix handlying of typing.Any
Browse files Browse the repository at this point in the history
So far Any was not taken in consideration and it was failing
to handle it correctly. If the base_type is Any, stop the
recursive check and return as if everything is fine.

Signed-off-by: Erik-Cristian Seulean <[email protected]>
  • Loading branch information
erikseulean committed Apr 1, 2020
1 parent 19ce44b commit fd7572d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 1 addition & 3 deletions attrs_strict/_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def __init__(self, container, attribute):

def __str__(self):
error = "{} can not be empty and must be {} (got {})".format(
self.attribute.name,
self.attribute.type,
self.container,
self.attribute.name, self.attribute.type, self.container,
)

return self._render(error)
Expand Down
12 changes: 9 additions & 3 deletions attrs_strict/_type_validation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import collections
import typing

from ._error import AttributeTypeError, BadTypeError, EmptyError, TupleError, UnionError
from ._error import (
AttributeTypeError,
BadTypeError,
EmptyError,
TupleError,
UnionError,
)


def type_validator(empty_ok=True):
Expand Down Expand Up @@ -31,7 +37,7 @@ def _validate_elements(attribute, value, expected_type):
else expected_type
)

if base_type is None:
if base_type is None or base_type == typing.Any:
return

if base_type != typing.Union and not isinstance(value, base_type):
Expand All @@ -54,7 +60,7 @@ def _validate_elements(attribute, value, expected_type):


def _handle_set_or_list(attribute, container, expected_type):
element_type, = expected_type.__args__
(element_type,) = expected_type.__args__

for element in container:
try:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
from typing import DefaultDict, List
from typing import Any, Dict, DefaultDict, List

import pytest

Expand Down Expand Up @@ -43,3 +43,15 @@ def test_defaultdict_with_correct_type_no_raise():
attr.type = DefaultDict[int, List[int]]

validator(None, attr, elem)


def test_dict_with_any_does_not_raise():
elem = {"foo": 123, "b": "abc"}

validator = type_validator()

attr = MagicMock()
attr.name = "zoo"
attr.type = Dict[str, Any]

validator(None, attr, elem)
4 changes: 3 additions & 1 deletion tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Set, Tuple
from typing import Any, Dict, List, Set, Tuple

import pytest

Expand Down Expand Up @@ -80,6 +80,8 @@ def test_list_of_values_raise_value_error(values, type_, error_message):
([[1], [2], [3]], List[List[int]]),
({1, 2, 3}, Set[int]),
([{1: [1, 2, 3], 2: [3, 4, 5]}], List[Dict[int, List[int]]]),
([1, 2, 3, 4], List[Any]),
([1, 2, {"foo": "bar"}], List[Any]),
],
)
def test_list_of_valid_values_no_raise(values, type_):
Expand Down

0 comments on commit fd7572d

Please sign in to comment.