Skip to content

Commit

Permalink
Unify related types together in class SimilarTypes (bloomberg#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Raab <[email protected]>
  • Loading branch information
saytosid authored and raabf committed May 22, 2020
1 parent 02125e8 commit dc98a83
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions attrs_strict/_type_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
from collections import MutableMapping


class SimilarTypes:
Dict = {
dict,
collections.OrderedDict,
collections.defaultdict,
Mapping,
MutableMapping,
typing.Dict,
typing.DefaultDict,
typing.Mapping,
typing.MutableMapping,
}
List = {set, list, typing.List, typing.Set}
Tuple = {tuple, typing.Tuple}


def type_validator(empty_ok=True):
"""
Validates the attributes using the type argument specified. If the
Expand Down Expand Up @@ -54,21 +70,11 @@ def _validate_elements(attribute, value, expected_type):
if base_type != typing.Union and not isinstance(value, base_type):
raise AttributeTypeError(value, attribute)

if base_type in {set, list, typing.List, typing.Set}:
if base_type in SimilarTypes.List:
_handle_set_or_list(attribute, value, expected_type)
elif base_type in {
dict,
collections.OrderedDict,
collections.defaultdict,
Mapping,
MutableMapping,
typing.Dict,
typing.DefaultDict,
typing.Mapping,
typing.MutableMapping,
}:
elif base_type in SimilarTypes.Dict:
_handle_dict(attribute, value, expected_type)
elif base_type in {tuple, typing.Tuple}:
elif base_type in SimilarTypes.Tuple:
_handle_tuple(attribute, value, expected_type)
elif base_type == typing.Union:
_handle_union(attribute, value, expected_type)
Expand Down

0 comments on commit dc98a83

Please sign in to comment.