Skip to content
Merged
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
29 changes: 15 additions & 14 deletions injection/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ from enum import Enum
from logging import Logger
from typing import Any, Final, Protocol, Self, final, overload, runtime_checkable

from typing_extensions import TypeForm

from ._core.asfunction import AsFunctionWrappedType as _AsFunctionWrappedType
from ._core.common.invertible import Invertible as _Invertible
from ._core.common.type import InputType as _InputType
from ._core.common.type import TypeInfo as _TypeInfo
from ._core.locator import InjectableFactory as _InjectableFactory
from ._core.locator import ModeStr
Expand Down Expand Up @@ -124,7 +125,7 @@ class _BoundMappedScope:
class LazyInstance[T]:
def __init__(
self,
cls: _InputType[T],
cls: TypeForm[T],
/,
default: T = ...,
*,
Expand All @@ -148,7 +149,7 @@ class Module:

name: str | None

def __contains__(self, cls: _InputType[Any], /) -> bool: ...
def __contains__(self, cls: TypeForm[Any], /) -> bool: ...
@property
def is_locked(self) -> bool: ...
@overload
Expand Down Expand Up @@ -304,7 +305,7 @@ class Module:

def reserve_scoped_slot[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
/,
scope_name: str,
*,
Expand All @@ -324,13 +325,13 @@ class Module:
) -> Callable[..., Awaitable[T]]: ...
async def afind_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
*,
threadsafe: bool | None = ...,
) -> T: ...
def find_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
*,
threadsafe: bool | None = ...,
) -> T:
Expand All @@ -342,23 +343,23 @@ class Module:
@overload
async def aget_instance[T, Default](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: Default,
*,
threadsafe: bool | None = ...,
) -> T | Default: ...
@overload
async def aget_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: T = ...,
*,
threadsafe: bool | None = ...,
) -> T: ...
@overload
def get_instance[T, Default](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: Default,
*,
threadsafe: bool | None = ...,
Expand All @@ -371,31 +372,31 @@ class Module:
@overload
def get_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: T = ...,
*,
threadsafe: bool | None = ...,
) -> T: ...
@overload
def aget_lazy_instance[T, Default](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: Default,
*,
threadsafe: bool | None = ...,
) -> Awaitable[T | Default]: ...
@overload
def aget_lazy_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: T = ...,
*,
threadsafe: bool | None = ...,
) -> Awaitable[T]: ...
@overload
def get_lazy_instance[T, Default](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: Default,
*,
threadsafe: bool | None = ...,
Expand All @@ -411,7 +412,7 @@ class Module:
@overload
def get_lazy_instance[T](
self,
cls: _InputType[T],
cls: TypeForm[T],
default: T = ...,
*,
threadsafe: bool | None = ...,
Expand Down
7 changes: 3 additions & 4 deletions injection/_core/common/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
Iterator,
)
from inspect import isclass, isfunction
from types import GenericAlias, UnionType
from typing import (
Any,
TypeAliasType,
get_args,
get_origin,
get_type_hints,
)

type InputType[T] = type[T] | TypeAliasType | GenericAlias | UnionType
from typing_extensions import TypeForm

type TypeInfo[T] = (
InputType[T]
TypeForm[T]
| Callable[..., T]
| Callable[..., Awaitable[T]]
| Collection[TypeInfo[T]]
Expand Down
5 changes: 3 additions & 2 deletions injection/_core/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from types import MappingProxyType
from typing import Any, Self, get_type_hints

from typing_extensions import TypeForm

from injection._core.common.invertible import Invertible
from injection._core.common.type import InputType
from injection._core.module import Module, mod
from injection._core.scope import ScopeKind, ScopeKindStr, adefine_scope, define_scope
from injection._core.slots import SlotKey
Expand Down Expand Up @@ -103,7 +104,7 @@ class LazyInstance[T]:

def __init__(
self,
cls: InputType[T],
cls: TypeForm[T],
/,
default: T = NotImplemented,
*,
Expand Down
5 changes: 3 additions & 2 deletions injection/_core/injectables.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
runtime_checkable,
)

from typing_extensions import TypeForm

from injection._core.common.asynchronous import AsyncLock, Caller
from injection._core.common.type import InputType
from injection._core.scope import Scope, get_first_scope, in_scope_cache
from injection._core.slots import SlotKey
from injection.exceptions import EmptySlotError, InjectionError
Expand Down Expand Up @@ -202,7 +203,7 @@ def build(self, scope: Scope) -> T:

@dataclass(repr=False, eq=False, frozen=True, slots=True)
class ScopedSlotInjectable[T](Injectable[T]):
cls: InputType[T]
cls: TypeForm[T]
scope_name: str
key: SlotKey[T] = field(default_factory=SlotKey)

Expand Down
19 changes: 10 additions & 9 deletions injection/_core/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
)
from weakref import WeakKeyDictionary

from typing_extensions import TypeForm

from injection._core.common.asynchronous import AsyncCaller, Caller, SyncCaller
from injection._core.common.event import Event, EventChannel, EventListener
from injection._core.common.type import InputType
from injection._core.injectables import Injectable
from injection.exceptions import NoInjectable, SkipInjectable

Expand All @@ -30,7 +31,7 @@ class LocatorEvent(Event, ABC):

@dataclass(frozen=True, slots=True)
class LocatorDependenciesUpdated[T](LocatorEvent):
classes: Collection[InputType[T]]
classes: Collection[TypeForm[T]]
mode: Mode

def __str__(self) -> str:
Expand Down Expand Up @@ -155,7 +156,7 @@ class Record[T](NamedTuple):

@dataclass(repr=False, eq=False, frozen=True, kw_only=True, slots=True)
class Updater[T]:
classes: Collection[InputType[T]]
classes: Collection[TypeForm[T]]
broker: InjectableBroker[T]
mode: Mode

Expand All @@ -165,7 +166,7 @@ def make_record(self) -> Record[T]:

@dataclass(repr=False, frozen=True, slots=True)
class Locator:
__records: dict[InputType[Any], Record[Any]] = field(
__records: dict[TypeForm[Any], Record[Any]] = field(
default_factory=dict,
init=False,
)
Expand All @@ -174,7 +175,7 @@ class Locator:
init=False,
)

def __contains__(self, cls: InputType[Any], /) -> bool:
def __contains__(self, cls: TypeForm[Any], /) -> bool:
return cls in self.__records

@property
Expand All @@ -186,7 +187,7 @@ def is_locked(self, provider: InjectionProvider) -> bool:

def request[T](
self,
cls: InputType[T],
cls: TypeForm[T],
/,
provider: InjectionProvider,
) -> Injectable[T]:
Expand Down Expand Up @@ -242,9 +243,9 @@ def __iter_injectables(

def __prepare_for_updating[T](
self,
classes: Iterable[InputType[T]],
classes: Iterable[TypeForm[T]],
record: Record[T],
) -> Iterator[tuple[InputType[T], Record[T]]]:
) -> Iterator[tuple[TypeForm[T], Record[T]]]:
for cls in classes:
try:
existing = self.__records[cls]
Expand All @@ -260,7 +261,7 @@ def __prepare_for_updating[T](
def __keep_new_record[T](
new: Record[T],
existing: Record[T],
cls: InputType[T],
cls: TypeForm[T],
) -> bool:
new_mode, existing_mode = new.mode, existing.mode

Expand Down
Loading
Loading