Skip to content

Commit f5210f2

Browse files
authored
refactoring: ⚡️ Doesn't place functions decorated with overload in memory
1 parent e42a9c9 commit f5210f2

4 files changed

Lines changed: 335 additions & 300 deletions

File tree

injection/_core/module.py

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from logging import Logger, getLogger
3030
from types import MethodType
3131
from typing import (
32+
TYPE_CHECKING,
3233
Any,
3334
AsyncContextManager,
3435
ClassVar,
@@ -577,21 +578,23 @@ def decorator(wp: Callable[P, T]) -> Callable[P, T]:
577578

578579
return decorator(wrapped) if wrapped else decorator
579580

580-
@overload
581-
def make_injected_function[**P, T](
582-
self,
583-
wrapped: Callable[P, T],
584-
/,
585-
threadsafe: bool | None = ...,
586-
) -> SyncInjectedFunction[P, T]: ...
581+
if TYPE_CHECKING: # pragma: no cover
587582

588-
@overload
589-
def make_injected_function[**P, T](
590-
self,
591-
wrapped: Callable[P, Awaitable[T]],
592-
/,
593-
threadsafe: bool | None = ...,
594-
) -> AsyncInjectedFunction[P, T]: ...
583+
@overload
584+
def make_injected_function[**P, T](
585+
self,
586+
wrapped: Callable[P, T],
587+
/,
588+
threadsafe: bool | None = ...,
589+
) -> SyncInjectedFunction[P, T]: ...
590+
591+
@overload
592+
def make_injected_function[**P, T](
593+
self,
594+
wrapped: Callable[P, Awaitable[T]],
595+
/,
596+
threadsafe: bool | None = ...,
597+
) -> AsyncInjectedFunction[P, T]: ...
595598

596599
def make_injected_function[**P, T](
597600
self,
@@ -643,23 +646,25 @@ def find_instance[T](
643646
injectable = self[cls]
644647
return injectable.get_instance()
645648

646-
@overload
647-
async def aget_instance[T, Default](
648-
self,
649-
cls: InputType[T],
650-
default: Default,
651-
*,
652-
threadsafe: bool | None = ...,
653-
) -> T | Default: ...
654-
655-
@overload
656-
async def aget_instance[T](
657-
self,
658-
cls: InputType[T],
659-
default: T = ...,
660-
*,
661-
threadsafe: bool | None = ...,
662-
) -> T: ...
649+
if TYPE_CHECKING: # pragma: no cover
650+
651+
@overload
652+
async def aget_instance[T, Default](
653+
self,
654+
cls: InputType[T],
655+
default: Default,
656+
*,
657+
threadsafe: bool | None = ...,
658+
) -> T | Default: ...
659+
660+
@overload
661+
async def aget_instance[T](
662+
self,
663+
cls: InputType[T],
664+
default: T = ...,
665+
*,
666+
threadsafe: bool | None = ...,
667+
) -> T: ...
663668

664669
async def aget_instance[T, Default](
665670
self,
@@ -673,23 +678,25 @@ async def aget_instance[T, Default](
673678
except (KeyError, SkipInjectable):
674679
return default
675680

676-
@overload
677-
def get_instance[T, Default](
678-
self,
679-
cls: InputType[T],
680-
default: Default,
681-
*,
682-
threadsafe: bool | None = ...,
683-
) -> T | Default: ...
684-
685-
@overload
686-
def get_instance[T](
687-
self,
688-
cls: InputType[T],
689-
default: T = ...,
690-
*,
691-
threadsafe: bool | None = ...,
692-
) -> T: ...
681+
if TYPE_CHECKING: # pragma: no cover
682+
683+
@overload
684+
def get_instance[T, Default](
685+
self,
686+
cls: InputType[T],
687+
default: Default,
688+
*,
689+
threadsafe: bool | None = ...,
690+
) -> T | Default: ...
691+
692+
@overload
693+
def get_instance[T](
694+
self,
695+
cls: InputType[T],
696+
default: T = ...,
697+
*,
698+
threadsafe: bool | None = ...,
699+
) -> T: ...
693700

694701
def get_instance[T, Default](
695702
self,
@@ -703,23 +710,25 @@ def get_instance[T, Default](
703710
except (KeyError, SkipInjectable):
704711
return default
705712

706-
@overload
707-
def aget_lazy_instance[T, Default](
708-
self,
709-
cls: InputType[T],
710-
default: Default,
711-
*,
712-
threadsafe: bool | None = ...,
713-
) -> Awaitable[T | Default]: ...
714-
715-
@overload
716-
def aget_lazy_instance[T](
717-
self,
718-
cls: InputType[T],
719-
default: T = ...,
720-
*,
721-
threadsafe: bool | None = ...,
722-
) -> Awaitable[T]: ...
713+
if TYPE_CHECKING: # pragma: no cover
714+
715+
@overload
716+
def aget_lazy_instance[T, Default](
717+
self,
718+
cls: InputType[T],
719+
default: Default,
720+
*,
721+
threadsafe: bool | None = ...,
722+
) -> Awaitable[T | Default]: ...
723+
724+
@overload
725+
def aget_lazy_instance[T](
726+
self,
727+
cls: InputType[T],
728+
default: T = ...,
729+
*,
730+
threadsafe: bool | None = ...,
731+
) -> Awaitable[T]: ...
723732

724733
def aget_lazy_instance[T, Default](
725734
self,
@@ -735,23 +744,25 @@ def aget_lazy_instance[T, Default](
735744
metadata = function.__inject_metadata__.set_owner(cls)
736745
return SimpleAwaitable(metadata.acall)
737746

738-
@overload
739-
def get_lazy_instance[T, Default](
740-
self,
741-
cls: InputType[T],
742-
default: Default,
743-
*,
744-
threadsafe: bool | None = ...,
745-
) -> Invertible[T | Default]: ...
746-
747-
@overload
748-
def get_lazy_instance[T](
749-
self,
750-
cls: InputType[T],
751-
default: T = ...,
752-
*,
753-
threadsafe: bool | None = ...,
754-
) -> Invertible[T]: ...
747+
if TYPE_CHECKING: # pragma: no cover
748+
749+
@overload
750+
def get_lazy_instance[T, Default](
751+
self,
752+
cls: InputType[T],
753+
default: Default,
754+
*,
755+
threadsafe: bool | None = ...,
756+
) -> Invertible[T | Default]: ...
757+
758+
@overload
759+
def get_lazy_instance[T](
760+
self,
761+
cls: InputType[T],
762+
default: T = ...,
763+
*,
764+
threadsafe: bool | None = ...,
765+
) -> Invertible[T]: ...
755766

756767
def get_lazy_instance[T, Default](
757768
self,

injection/_core/scope.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from enum import StrEnum
1111
from types import EllipsisType, TracebackType
1212
from typing import (
13+
TYPE_CHECKING,
1314
Any,
1415
AsyncContextManager,
1516
ContextManager,
@@ -158,12 +159,13 @@ def get_active_scopes(name: str) -> tuple[Scope, ...]:
158159
return tuple(itertools.chain.from_iterable(active_scopes))
159160

160161

161-
@overload
162-
def get_scope(name: str, default: EllipsisType = ...) -> Scope: ...
162+
if TYPE_CHECKING: # pragma: no cover
163163

164+
@overload
165+
def get_scope(name: str, default: EllipsisType = ...) -> Scope: ...
164166

165-
@overload
166-
def get_scope[T](name: str, default: T) -> Scope | T: ...
167+
@overload
168+
def get_scope[T](name: str, default: T) -> Scope | T: ...
167169

168170

169171
def get_scope[T](name: str, default: T | EllipsisType = ...) -> Scope | T:

injection/entrypoint.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from functools import wraps
88
from types import MethodType
99
from types import ModuleType as PythonModule
10-
from typing import Any, Concatenate, Self, final, overload
10+
from typing import TYPE_CHECKING, Any, Concatenate, Self, final, overload
1111

1212
from injection import Module
1313
from injection.loaders import ProfileLoader, PythonModuleLoader
@@ -21,13 +21,13 @@
2121
Entrypoint[EPP, T2],
2222
]
2323

24+
if TYPE_CHECKING: # pragma: no cover
2425

25-
@overload
26-
def autocall[T: Callable[..., Any]](wrapped: T, /) -> T: ...
26+
@overload
27+
def autocall[T: Callable[..., Any]](wrapped: T, /) -> T: ...
2728

28-
29-
@overload
30-
def autocall[T: Callable[..., Any]](wrapped: None = ..., /) -> Callable[[T], T]: ...
29+
@overload
30+
def autocall[T: Callable[..., Any]](wrapped: None = ..., /) -> Callable[[T], T]: ...
3131

3232

3333
def autocall[T: Callable[..., Any]](
@@ -44,26 +44,26 @@ def decorator(wp: T) -> T:
4444
# SMP = Setup Method Parameters
4545
# EPP = EntryPoint Parameters
4646

47+
if TYPE_CHECKING: # pragma: no cover
4748

48-
@overload
49-
def entrypointmaker[**SMP, **EPP, T1, T2](
50-
wrapped: EntrypointSetupMethod[SMP, EPP, T1, T2],
51-
/,
52-
*,
53-
profile_loader: ProfileLoader = ...,
54-
) -> EntrypointDecorator[EPP, T1, T2]: ...
55-
49+
@overload
50+
def entrypointmaker[**SMP, **EPP, T1, T2](
51+
wrapped: EntrypointSetupMethod[SMP, EPP, T1, T2],
52+
/,
53+
*,
54+
profile_loader: ProfileLoader = ...,
55+
) -> EntrypointDecorator[EPP, T1, T2]: ...
5656

57-
@overload
58-
def entrypointmaker[**SMP, **EPP, T1, T2](
59-
wrapped: None = ...,
60-
/,
61-
*,
62-
profile_loader: ProfileLoader = ...,
63-
) -> Callable[
64-
[EntrypointSetupMethod[SMP, EPP, T1, T2]],
65-
EntrypointDecorator[EPP, T1, T2],
66-
]: ...
57+
@overload
58+
def entrypointmaker[**SMP, **EPP, T1, T2](
59+
wrapped: None = ...,
60+
/,
61+
*,
62+
profile_loader: ProfileLoader = ...,
63+
) -> Callable[
64+
[EntrypointSetupMethod[SMP, EPP, T1, T2]],
65+
EntrypointDecorator[EPP, T1, T2],
66+
]: ...
6767

6868

6969
def entrypointmaker[**SMP, **EPP, T1, T2](

0 commit comments

Comments
 (0)