bloomberg / attrs-strict

Provides runtime validation of attributes specified in Python 'attr'-based data classes.
Apache License 2.0
52 stars 19 forks source link

Mapping type should be passed to _handle_dict #27

Closed anentropic closed 4 years ago

anentropic commented 4 years ago
elif base_type in {
        dict,
        collections.OrderedDict,
        collections.defaultdict,
        typing.Dict,
        typing.DefaultDict,
    }:
        _handle_dict(attribute, value, expected_type)

this list of types should include typing.Mapping

Mapping[<keyT>, <valueT>] are not caught by any of the checks so they fall-thru i.e. always succeed

anentropic commented 4 years ago

also, validating a type like Dict[str, Any] gives an error:

/VENV/lib/python2.7/site-packages/attrs_strict/_type_validation.pyc in _validate_elements(attribute, value, expected_type)
     35         return
     36
---> 37     if base_type != typing.Union and not isinstance(value, base_type):
     38         raise AttributeTypeError(value, attribute)
     39

/VENV/lib/python2.7/site-packages/typing.pyc in __instancecheck__(self, obj)
    632
    633     def __instancecheck__(self, obj):
--> 634         raise TypeError("Any cannot be used with isinstance().")
    635
    636     def __subclasscheck__(self, cls):

TypeError: Any cannot be used with isinstance().
erikseulean commented 4 years ago

Thanks for opening this @anentropic . I'll have a look at this and come back!

erikseulean commented 4 years ago

@anentropic I added the MutableMapping and Mapping types and fixed the typing.Any issue. Thanks for reporting this. I'll have a sweep through the collections library and improve the support for other types existing sometimes soon.

erikseulean commented 4 years ago

New version available, including this changes: https://pypi.org/project/attrs-strict/