Skip to content

Commit e235bae

Browse files
authored
fix: 🐛 Mypy check for asfunction
1 parent 03da590 commit e235bae

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

injection/__init__.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from enum import Enum
55
from logging import Logger
66
from typing import Any, Final, Protocol, Self, final, overload, runtime_checkable
77

8+
from ._core.asfunction import AsFunctionWrappedType as _AsFunctionWrappedType
89
from ._core.common.invertible import Invertible as _Invertible
910
from ._core.common.type import InputType as _InputType
1011
from ._core.common.type import TypeInfo as _TypeInfo
@@ -32,7 +33,7 @@ singleton = __MODULE.singleton
3233

3334
@overload
3435
def asfunction[**P, T](
35-
wrapped: type[Callable[P, T]],
36+
wrapped: _AsFunctionWrappedType[P, T],
3637
/,
3738
*,
3839
module: Module = ...,
@@ -45,7 +46,7 @@ def asfunction[**P, T](
4546
*,
4647
module: Module = ...,
4748
threadsafe: bool | None = ...,
48-
) -> Callable[[type[Callable[P, T]]], Callable[P, T]]: ...
49+
) -> Callable[[_AsFunctionWrappedType[P, T]], Callable[P, T]]: ...
4950
@asynccontextmanager
5051
def adefine_scope(
5152
name: str,

injection/_core/asfunction.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1+
from abc import abstractmethod
12
from collections.abc import Callable
23
from functools import wraps
34
from inspect import iscoroutinefunction
4-
from typing import Any
5+
from typing import Any, Protocol, runtime_checkable
56

67
from injection._core.common.asynchronous import Caller
78
from injection._core.module import Module, mod
89

10+
type AsFunctionWrappedType[**P, T] = type[_Callable[P, T]]
11+
12+
13+
@runtime_checkable
14+
class _Callable[**P, T](Protocol):
15+
__slots__ = ()
16+
17+
@abstractmethod
18+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T:
19+
raise NotImplementedError
20+
921

1022
def asfunction[**P, T](
11-
wrapped: type[Callable[P, T]] | None = None,
23+
wrapped: AsFunctionWrappedType[P, T] | None = None,
1224
/,
1325
*,
1426
module: Module | None = None,
1527
threadsafe: bool | None = None,
1628
) -> Any:
1729
module = module or mod()
1830

19-
def decorator(wp: type[Callable[P, T]]) -> Callable[P, T]:
31+
def decorator(wp: AsFunctionWrappedType[P, T]) -> Callable[P, T]:
2032
get_method = wp.__call__.__get__
2133
method = get_method(NotImplemented)
2234
factory: Caller[..., Callable[P, T]] = module.make_injected_function(

0 commit comments

Comments
 (0)