biqqles / dataclassy

A fast and flexible reimplementation of data classes
https://pypi.org/project/dataclassy
Mozilla Public License 2.0
81 stars 9 forks source link

TypeError: argument of type 'NoneType' is not iterable #39

Closed fubuloubu closed 3 years ago

fubuloubu commented 3 years ago

I'm not quite sure where this is happening in my application (to be able to give a MWE) but there appears to be an issue with using this library in Py3.6 (before the inclusion of dataclasses into stdlib):

  File "python3.6/site-packages/dataclassy/decorator.py", line 40, in dataclass
    return apply_metaclass(cls)
  File "python3.6/site-packages/dataclassy/decorator.py", line 35, in apply_metaclass
    return metaclass(to_class.__name__, to_class.__bases__, dict_, **options)
  File "python3.6/site-packages/dataclassy/dataclass.py", line 120, in __new__
    dict_.setdefault('__hash__', generate_hash(all_annotations))
  File "python3.6/site-packages/dataclassy/dataclass.py", line 170, in generate_hash
    hash_of = ', '.join(['self.__class__', *(f'self.{f}' for f, h in annotations.items() if Hashed.is_hinted(h))])
  File "python3.6/site-packages/dataclassy/dataclass.py", line 170, in <genexpr>
    hash_of = ', '.join(['self.__class__', *(f'self.{f}' for f, h in annotations.items() if Hashed.is_hinted(h))])
  File "python3.6/site-packages/dataclassy/dataclass.py", line 29, in is_hinted
    return (hasattr(hint, '__args__') and cls in hint.__args__ or
TypeError: argument of type 'NoneType' is not iterable

I think the breaking assumption is that hint.__args__ is not None in this case.

biqqles commented 3 years ago

Thanks for the report. You can also solve this with getattr(hint, '__args__', None), or better yet, isinstance(getattr(hint, '__args__', None), Iterable).

This made me realise I've been careless about Python 3.6 hinting in general. Since v0.8, subscripting Internal or Hashed as objects (instead of string literals or from future import annotations) will raise a TypeError because __class_getitem__ is not recognised.

fubuloubu commented 3 years ago

Good point, made that suggested change in e60e376

biqqles commented 3 years ago

Except you missed the import!