Skip to content

Commit

Permalink
feat: fix compatibility issue with python3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
PJCampi committed May 23, 2024
1 parent e656b29 commit 4c6ab39
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import sys
import warnings
from collections import defaultdict, namedtuple
from collections.abc import (Collection as ABCCollection, Mapping as ABCMapping, MutableMapping, MutableSequence,
MutableSet, Sequence, Set)
if sys.version_info >= (3, 9):
from collections.abc import (Collection as ABCCollection, Mapping as ABCMapping, MutableMapping, MutableSequence,
MutableSet, Sequence, Set)
else:
from typing import (Collection as ABCCollection, Mapping as ABCMapping, MutableMapping, MutableSequence,
MutableSet, Sequence, Set)

from dataclasses import (MISSING,
fields,
is_dataclass # type: ignore
Expand Down Expand Up @@ -34,7 +39,7 @@

confs = ['encoder', 'decoder', 'mm_field', 'letter_case', 'exclude']
FieldOverride = namedtuple('FieldOverride', confs) # type: ignore
collections_abc_mapping = MappingProxyType({
collections_abc_type_to_implementation_type = MappingProxyType({
ABCCollection: tuple,
ABCMapping: dict,
MutableMapping: dict,
Expand Down Expand Up @@ -322,7 +327,7 @@ def _decode_generic(type_, value, infer_missing):
materialize_type = type_

# map abstract collection to concrete implementation
materialize_type = collections_abc_mapping.get(materialize_type, materialize_type)
materialize_type = collections_abc_type_to_implementation_type.get(materialize_type, materialize_type)

res = materialize_type(xs)
elif _is_generic_dataclass(type_):
Expand Down
3 changes: 2 additions & 1 deletion tests/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from uuid import UUID
if sys.version_info >= (3, 9):
from collections import Counter
from collections.abc import Mapping, MutableMapping, MutableSequence, MutableSet, Sequence, Set as ABCSet
else:
from typing import Counter
from typing import Counter, Mapping, MutableMapping, MutableSequence, MutableSet, Sequence, Set as ABCSet

from marshmallow import fields

Expand Down

0 comments on commit 4c6ab39

Please sign in to comment.