Skip to content

Commit

Permalink
Merge pull request #16 from muhammetenes/fix_error_for_any
Browse files Browse the repository at this point in the history
fixed to error for any and anystr
  • Loading branch information
muhammetenes authored Feb 10, 2020
2 parents 0867f33 + 3af9601 commit d5e90d0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dataclass_plus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.7"
__version__ = "1.0.8"

import typing

Expand Down Expand Up @@ -71,10 +71,11 @@ def _validate_list(value: Any, target_type: Any) -> bool:
results = []
# List comprehension is not used because it has more performance in this way
for val in value:
if isinstance(target_type.__args__[0], typing._GenericAlias):
_is_valid(val, target_type.__args__[0])
if not isinstance(target_type.__args__[0], type):
is_valid = _is_valid(val, target_type.__args__[0])
else:
results.append(isinstance(val, target_type.__args__[0]))
is_valid = isinstance(val, target_type.__args__[0])
results.append(is_valid)
return all(results)


Expand All @@ -87,10 +88,11 @@ def _validate_tuple(value: Any, target_type: Any) -> bool:
results = []
# List comprehension is not used because it has more performance in this way
for index, val in enumerate(value):
if isinstance(target_type.__args__[index], typing._GenericAlias):
_is_valid(val, target_type.__args__[index])
if not isinstance(target_type.__args__[index], type):
is_valid = _is_valid(val, target_type.__args__[index])
else:
results.append(type(val) is target_type.__args__[index])
is_valid = type(val) is target_type.__args__[index]
results.append(is_valid)
return all(results)


Expand Down
50 changes: 50 additions & 0 deletions test/test_dataclass_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ class TypingListStringDataclass:
assert TypingListStringDataclass(typing_list=[["test"]])


def test_nested_typing_list_in_any_validator():
@dataclass_plus
class TypingListAnyDataclass:
typing_list: typing.List[typing.Any]

assert TypingListAnyDataclass(typing_list=[""])


def test_nested_typing_list_in_any_str_validator():
@dataclass_plus
class TypingListAnyStrDataclass:
typing_list: typing.List[typing.AnyStr]

assert TypingListAnyStrDataclass(typing_list=[b""])


def test_nested_typing_list_in_any_wrong_validator():
@dataclass_plus
class TypingListAnyStrDataclass:
typing_list: typing.List[typing.AnyStr]

with pytest.raises(TypeError):
assert TypingListAnyStrDataclass(typing_list=[1])


def test_typing_dict_string_validator():
@dataclass_plus
class TypingDictStringDataclass:
Expand Down Expand Up @@ -235,6 +260,31 @@ class TypingTupleIntDataclass:
assert TypingTupleIntDataclass(typing_tuple=("test", 1))


def test_typing_tuple_any_str_type_validator():
@dataclass_plus
class TypingTupleIntDataclass:
typing_tuple: typing.Tuple[typing.Any, int]

assert TypingTupleIntDataclass(typing_tuple=("test", 1))


def test_typing_tuple_anystr_str_type_validator():
@dataclass_plus
class TypingTupleIntDataclass:
typing_tuple: typing.Tuple[typing.AnyStr, int]

assert TypingTupleIntDataclass(typing_tuple=(b"test", 1))


def test_typing_tuple_anystr_str_wrong_type_validator():
@dataclass_plus
class TypingTupleIntDataclass:
typing_tuple: typing.Tuple[typing.AnyStr, int]

with pytest.raises(TypeError):
assert TypingTupleIntDataclass(typing_tuple=(1, 1))


def test_typing_tuple_int_typing_list_validator():
@dataclass_plus
class TypingTupleIntDataclass:
Expand Down

0 comments on commit d5e90d0

Please sign in to comment.