Skip to content

Commit

Permalink
Fix collections.abc.Mapping handling on 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Jul 24, 2024
1 parent d231ede commit 9f0d5c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/cattrs/_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
from collections import deque
from collections.abc import Mapping as AbcMapping
from collections.abc import MutableMapping as AbcMutableMapping
from collections.abc import MutableSet as AbcMutableSet
from collections.abc import Set as AbcSet
from dataclasses import MISSING, Field, is_dataclass
Expand Down Expand Up @@ -219,8 +221,6 @@ def get_final_base(type) -> Optional[type]:

if sys.version_info >= (3, 9):
from collections import Counter
from collections.abc import Mapping as AbcMapping
from collections.abc import MutableMapping as AbcMutableMapping
from collections.abc import MutableSequence as AbcMutableSequence
from collections.abc import MutableSet as AbcMutableSet
from collections.abc import Sequence as AbcSequence
Expand Down
45 changes: 20 additions & 25 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from collections import Counter, deque
from collections.abc import Mapping as AbcMapping
from collections.abc import MutableMapping as AbcMutableMapping
from collections.abc import MutableSet as AbcMutableSet
from dataclasses import Field
from enum import Enum
Expand Down Expand Up @@ -288,12 +290,10 @@ def unstruct_strat(self) -> UnstructureStrategy:
)

@overload
def register_unstructure_hook(self) -> Callable[[UnstructureHook], None]:
...
def register_unstructure_hook(self) -> Callable[[UnstructureHook], None]: ...

@overload
def register_unstructure_hook(self, cls: Any, func: UnstructureHook) -> None:
...
def register_unstructure_hook(self, cls: Any, func: UnstructureHook) -> None: ...

def register_unstructure_hook(
self, cls: Any = None, func: UnstructureHook | None = None
Expand Down Expand Up @@ -341,26 +341,22 @@ def register_unstructure_hook_func(
@overload
def register_unstructure_hook_factory(
self, predicate: Predicate
) -> Callable[[UnstructureHookFactory], UnstructureHookFactory]:
...
) -> Callable[[UnstructureHookFactory], UnstructureHookFactory]: ...

@overload
def register_unstructure_hook_factory(
self, predicate: Predicate
) -> Callable[[ExtendedUnstructureHookFactory], ExtendedUnstructureHookFactory]:
...
) -> Callable[[ExtendedUnstructureHookFactory], ExtendedUnstructureHookFactory]: ...

@overload
def register_unstructure_hook_factory(
self, predicate: Predicate, factory: UnstructureHookFactory
) -> UnstructureHookFactory:
...
) -> UnstructureHookFactory: ...

@overload
def register_unstructure_hook_factory(
self, predicate: Predicate, factory: ExtendedUnstructureHookFactory
) -> ExtendedUnstructureHookFactory:
...
) -> ExtendedUnstructureHookFactory: ...

def register_unstructure_hook_factory(self, predicate, factory=None):
"""
Expand Down Expand Up @@ -429,12 +425,10 @@ def get_unstructure_hook(
)

@overload
def register_structure_hook(self) -> Callable[[StructureHook], None]:
...
def register_structure_hook(self) -> Callable[[StructureHook], None]: ...

@overload
def register_structure_hook(self, cl: Any, func: StructuredValue) -> None:
...
def register_structure_hook(self, cl: Any, func: StructuredValue) -> None: ...

def register_structure_hook(
self, cl: Any, func: StructureHook | None = None
Expand Down Expand Up @@ -484,26 +478,22 @@ def register_structure_hook_func(
@overload
def register_structure_hook_factory(
self, predicate: Predicate
) -> Callable[[StructureHookFactory, StructureHookFactory]]:
...
) -> Callable[[StructureHookFactory, StructureHookFactory]]: ...

@overload
def register_structure_hook_factory(
self, predicate: Predicate
) -> Callable[[ExtendedStructureHookFactory, ExtendedStructureHookFactory]]:
...
) -> Callable[[ExtendedStructureHookFactory, ExtendedStructureHookFactory]]: ...

@overload
def register_structure_hook_factory(
self, predicate: Predicate, factory: StructureHookFactory
) -> StructureHookFactory:
...
) -> StructureHookFactory: ...

@overload
def register_structure_hook_factory(
self, predicate: Predicate, factory: ExtendedStructureHookFactory
) -> ExtendedStructureHookFactory:
...
) -> ExtendedStructureHookFactory: ...

def register_structure_hook_factory(self, predicate, factory=None):
"""
Expand Down Expand Up @@ -1302,7 +1292,12 @@ def gen_structure_counter(self, cl: Any) -> MappingStructureFn[T]:

def gen_structure_mapping(self, cl: Any) -> MappingStructureFn[T]:
structure_to = get_origin(cl) or cl
if structure_to in (MutableMapping, Mapping): # These default to dicts
if structure_to in (
MutableMapping,
AbcMutableMapping,
Mapping,
AbcMapping,
): # These default to dicts
structure_to = dict
h = make_mapping_structure_fn(
cl, self, structure_to, detailed_validation=self.detailed_validation
Expand Down

0 comments on commit 9f0d5c4

Please sign in to comment.