Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# History

## 23.2.1 (UNRELEASED)

- Fix unnecessary `typing_extensions` import on Python 3.11.
([#446](https://github.com/python-attrs/cattrs/issues/446))

## 23.2.0 (2023-11-17)

- **Potentially breaking**: skip _attrs_ fields marked as `init=False` by default. This change is potentially breaking for unstructuring.
Expand Down
27 changes: 14 additions & 13 deletions src/cattrs/dispatch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

from functools import lru_cache, partial, singledispatch
from typing import Any, Callable, Dict, Generic, List, Optional, Tuple, TypeVar, Union
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar

from attrs import Factory, define, field
from typing_extensions import TypeAlias

if TYPE_CHECKING:
from typing_extensions import TypeAlias

T = TypeVar("T")

Expand Down Expand Up @@ -32,8 +36,8 @@ class FunctionDispatch:
objects that help determine dispatch should be instantiated objects.
"""

_handler_pairs: List[
Tuple[Callable[[Any], bool], Callable[[Any, Any], Any], bool]
_handler_pairs: list[
tuple[Callable[[Any], bool], Callable[[Any, Any], Any], bool]
] = Factory(list)

def register(
Expand All @@ -44,7 +48,7 @@ def register(
) -> None:
self._handler_pairs.insert(0, (can_handle, func, is_generator))

def dispatch(self, typ: Any) -> Optional[Callable[..., Any]]:
def dispatch(self, typ: Any) -> Callable[..., Any] | None:
"""
Return the appropriate handler for the object passed.
"""
Expand All @@ -66,7 +70,7 @@ def dispatch(self, typ: Any) -> Optional[Callable[..., Any]]:
def get_num_fns(self) -> int:
return len(self._handler_pairs)

def copy_to(self, other: "FunctionDispatch", skip: int = 0) -> None:
def copy_to(self, other: FunctionDispatch, skip: int = 0) -> None:
other._handler_pairs = self._handler_pairs[:-skip] + other._handler_pairs


Expand All @@ -84,7 +88,7 @@ class MultiStrategyDispatch(Generic[Hook]):
"""

_fallback_factory: HookFactory[Hook]
_direct_dispatch: Dict = field(init=False, factory=dict)
_direct_dispatch: dict = field(init=False, factory=dict)
_function_dispatch: FunctionDispatch = field(init=False, factory=FunctionDispatch)
_single_dispatch: Any = field(
init=False, factory=partial(singledispatch, _DispatchNotFound)
Expand Down Expand Up @@ -123,11 +127,8 @@ def register_cls_list(self, cls_and_handler, direct: bool = False) -> None:

def register_func_list(
self,
pred_and_handler: List[
Union[
Tuple[Callable[[Any], bool], Any],
Tuple[Callable[[Any], bool], Any, bool],
]
pred_and_handler: list[
tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool]
],
):
"""
Expand Down Expand Up @@ -156,7 +157,7 @@ def clear_cache(self) -> None:
def get_num_fns(self) -> int:
return self._function_dispatch.get_num_fns()

def copy_to(self, other: "MultiStrategyDispatch", skip: int = 0) -> None:
def copy_to(self, other: MultiStrategyDispatch, skip: int = 0) -> None:
self._function_dispatch.copy_to(other._function_dispatch, skip=skip)
for cls, fn in self._single_dispatch.registry.items():
other._single_dispatch.register(cls, fn)
Expand Down