dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

`typing` module types without parameters broken when using Python 3.9

Open Letsch22 opened this issue 3 years ago • 1 comments

When using types in the built-in typing module, if a type supports additional parameters and those parameters are not used, errors result when using Python 3.9

Code:

from dataclasses import dataclass
from typing import List

from dataclasses_json import DataClassJsonMixin


@dataclass
class Parent(DataClassJsonMixin):
    children: List


if __name__ == '__main__':
    p = Parent.from_dict({
        "children": ['hi']
    })
    print(p.to_json())

Output when running dataclasses-json 0.5.6 and Python 3.9:

  File "/Users/daniel.letscher/code/playground/dataclass.py", line 13, in <module>
    p = Parent.from_dict({
  File "/Users/daniel.letscher/code/playground/venv39/lib/python3.9/site-packages/dataclasses_json/api.py", line 82, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "/Users/daniel.letscher/code/playground/venv39/lib/python3.9/site-packages/dataclasses_json/core.py", line 201, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
  File "/Users/daniel.letscher/code/playground/venv39/lib/python3.9/site-packages/dataclasses_json/core.py", line 258, in _decode_generic
    xs = _decode_items(type_.__args__[0], value, infer_missing)
  File "/Users/daniel.letscher/.pyenv/versions/3.9.7/lib/python3.9/typing.py", line 706, in __getattr__
    raise AttributeError(attr)
AttributeError: __args__

Output when running dataclasses-json 0.5.6 and Python 3.8:

{"children": ["hi"]}

Letsch22 avatar Feb 25 '22 19:02 Letsch22

Added tests covering that case. Before the fixes they fail on the following: bug-341-test-reports.zip

Failed tests summary

Python 3.6:

  • list
  • Set
  • set
  • Tuple
  • tuple
  • FrozenSet
  • frozenset
  • collections.deque

Python 3.7 -- 3.8:

  • list
  • Dict
  • dict
  • set
  • Tuple
  • tuple
  • frozenset
  • collections.deque

Python 3.9 -- 3.10

  • List
  • list
  • Set
  • set
  • Tuple
  • tuple
  • FrozenSet
  • frozenset
  • collections.deque

USSX-Hares avatar Mar 20 '22 16:03 USSX-Hares