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

#341: Fixes for type-parameters without arguments

Open USSX-Hares opened this issue 2 years ago • 2 comments

Closes #341

Changes:

  • Updated tests.entities and tests.test_collections to include alternative forms of the same collection types (like builtins.list ("builtin") and typing.List (~~"generic"~~ "unbound") in addition to the existing typing.List[TypeA])
  • Added new internal helper class ~~core._NoArgs~~ utils._NoArgs and its singleton instance ~~core._NO_ARGS~~ utils._NO_ARGS
  • Added new methods ~~core._extract_args()~~ utils._get_type_args() and ~~core._extract_type_parameter()~~ utils._get_type_arg_param() which return either tp.__args__ value or _NO_ARGS constant
  • All "unsafe" usages of tp.__args__ replaced with the new methods
  • Added TypeVar special case check for core._decode_dict_keys()
    • Note: This is a special case for Python 3.7 and Python 3.8. By some reason, "unbound" dicts are counted as having key type parameter to be TypeVar('KT')
  • Added tp.__origin__ == None special case for utils._get_type_origin()
    • Note: This is a special case for Python 3.6 exclusively. By some reason, some "unbound" types (typing.Set, typing.Tuple, and typing.FrozenSet) are considered to have this field with None rather than generating an AttributeError
    • Same for tp.__args__ and dicts

Status

  • Without the fix, new tests fail on all versions of Python, but fail differently (see report in the comments of #341)
  • By now, there are no failures related to the missing tp.__args__:
    • [x] Python 3.6: ~~Decodes "unbound" (but not "builtin") sets, tuples, and frozensets as lists rather than required types~~
    • [x] Python 3.7-3.8: ~~Fails to decode "unbound" and "builtin" dicts by trying to instantiate uninstantiatable typing.Dict.~~ Was actually caused by the strange structure of dict class nesting in specifically these Python versions, see note above
    • [x] No longer fails on Python 3.9+
    • [x] (New) Python 3.6: ~~Unable to decode "unbound" typing.Dict and dict keys on Python 3.6 exclusively~~

USSX-Hares avatar Mar 22 '22 14:03 USSX-Hares

This seems like a pretty solid thing to get into the project so it is working in 3.10 as well :)

UberMeatShield avatar Jul 18 '22 13:07 UberMeatShield

Yes I'm sorry I've been MIA... I'll take a look soon!

lidatong avatar Jul 18 '22 16:07 lidatong

@lidatong do you have plans to release a new version with this patch in? It will be great if we can get this bug fixed

cprasad1 avatar Sep 15 '22 23:09 cprasad1

@lidatong any update on this? Would be great to have a new version released with these features.

rucas avatar Mar 28 '23 18:03 rucas