Skip to content

Commit 71449db

Browse files
author
remimd
committed
refactor: Now analyze type hints with type-analyzer
1 parent 3e75f29 commit 71449db

File tree

3 files changed

+154
-132
lines changed

3 files changed

+154
-132
lines changed

cq/_core/handler.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from collections.abc import Awaitable, Callable, Iterator
44
from dataclasses import dataclass, field
55
from functools import partial
6-
from inspect import Parameter, getmro, isclass
6+
from inspect import Parameter, isclass
77
from inspect import signature as inspect_signature
88
from typing import TYPE_CHECKING, Any, Protocol, Self, overload, runtime_checkable
99

1010
import injection
11+
from type_analyzer import MatchingTypesConfig, matching_types
1112

1213
type HandlerType[**P, T] = type[Handler[P, T]]
1314
type HandlerFactory[**P, T] = Callable[..., Awaitable[Handler[P, T]]]
@@ -49,7 +50,7 @@ def handlers_from(
4950
self,
5051
input_type: type[I],
5152
) -> Iterator[Callable[[I], Awaitable[O]]]:
52-
for it in getmro(input_type):
53+
for it in _standardize_input_type(input_type):
5354
for factory in self.__factories.get(it, ()):
5455
yield _make_handle_function(factory)
5556

@@ -69,7 +70,7 @@ def handlers_from(
6970
self,
7071
input_type: type[I],
7172
) -> Iterator[Callable[[I], Awaitable[O]]]:
72-
for it in getmro(input_type):
73+
for it in _standardize_input_type(input_type):
7374
factory = self.__factories.get(it, None)
7475
if factory is not None:
7576
yield _make_handle_function(factory)
@@ -170,6 +171,15 @@ def _resolve_input_type[I, O](handler_type: HandlerType[[I], O]) -> type[I]:
170171
)
171172

172173

174+
def _standardize_input_type(input_type: Any) -> tuple[Any, ...]:
175+
config = MatchingTypesConfig(
176+
with_bases=True,
177+
with_origin=True,
178+
with_type_alias_value=True,
179+
)
180+
return matching_types(input_type, config)
181+
182+
173183
def _make_handle_function[I, O](
174184
factory: HandlerFactory[[I], O],
175185
) -> Callable[[I], Awaitable[O]]:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ classifiers = [
5050
dependencies = [
5151
"anyio",
5252
"python-injection",
53+
"type-analyzer",
5354
]
5455

5556
[project.urls]

0 commit comments

Comments
 (0)