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: Wilfred Wong <[email protected]>
  • Loading branch information
saytosid authored and keiclone committed May 26, 2020
1 parent ca89de0 commit faa5c43
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 @@ -17,6 +17,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 @@ -50,21 +66,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 faa5c43

Please sign in to comment.