lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

Fix PEP 0673 before 3.11 #487

Closed george-zubrienko closed 9 months ago

george-zubrienko commented 9 months ago

Fixes #459

Working example on 3.10

from dataclasses_json import dataclass_json
@dataclass_json
@dataclass
class SpecialLinkedList:
    val: int
    nexts: list['SpecialLinkedList'] = None

my_list = SpecialLinkedList(val=1, nexts=[SpecialLinkedList(val=2)])
print(SpecialLinkedList.from_dict(my_list.to_dict()))
print(my_list)

SpecialLinkedList(val=1, nexts=[SpecialLinkedList(val=2, nexts=None)])
SpecialLinkedList(val=1, nexts=[SpecialLinkedList(val=2, nexts=None)])

Approach

Since before 3.10 we get the type as a plain string, and it is not a fully-qualified import reference, the only option I see is to go over all imported modules and try find the type string there. If that fails, we admit the failure and print a warning to the use saying expected outcome will not the as expected. This code only runs for versions <3.11

george-zubrienko commented 9 months ago

Will finalize this week - sorry for all the waiting, tough times.

github-actions[bot] commented 9 months ago

Coverage

Coverage Report
FileStmtsMissCoverMissing
dataclasses_json
   cfg.py51492%80, 84–86
   core.py2581793%39–42, 52, 65, 67, 82, 84, 170, 198, 378–385, 389
   mm.py2053085%33–36, 42–45, 53–56, 62–65, 88, 170–171, 176, 180, 184, 189, 193, 197, 205, 211, 216, 225, 230, 235, 244, 253–260
   stringcase.py25388%59, 76, 97
   undefined.py146299%25, 39
   utils.py1313673%12–25, 45–50, 61–65, 75, 100–101, 109–110, 125–133, 163, 182, 207
tests
   entities.py239399%22, 234, 240
   test_annotations.py814248%50–67, 78–102, 106–122
   test_api.py142497%88, 99, 139–140
   test_str_subclass.py22195%9
   test_union.py1311092%97–104, 118–125
TOTAL264515294% 

Tests Skipped Failures Errors Time
305 3 :zzz: 0 :x: 0 :fire: 6.249s :stopwatch: