diff --git a/di/_local_scope_context.py b/di/_local_scope_context.py index 08e097e5..f8d2dd5f 100644 --- a/di/_local_scope_context.py +++ b/di/_local_scope_context.py @@ -12,6 +12,7 @@ class LocalScopeContext(FusedContextManager[None]): + __slots__ = ("context", "scope", "token", "_sync_cm", "_async_cm") context: contextvars.ContextVar[ContainerState] scope: Scope token: contextvars.Token[ContainerState] diff --git a/di/_scope_map.py b/di/_scope_map.py index a6c0ecbe..f507a72f 100644 --- a/di/_scope_map.py +++ b/di/_scope_map.py @@ -25,6 +25,8 @@ class ScopeMap(Generic[KT, VT]): ChainMap also doesn't allow you to set values anywhere but the left mapping, and we need to set values in arbitrary mappings. """ + __slots__ = ("mappings",) + mappings: Dict[Scope, Dict[KT, VT]] def __init__(self) -> None: diff --git a/di/_state.py b/di/_state.py index 91f18db0..3449360d 100644 --- a/di/_state.py +++ b/di/_state.py @@ -27,6 +27,7 @@ class ContainerState(object): + __slots__ = ("binds", "cached_values", "stacks") binds: Dict[DependencyProvider, DependantProtocol[Any]] cached_values: ScopeMap[DependencyProvider, Any] stacks: Dict[Scope, Union[AsyncExitStack, ExitStack]] @@ -72,6 +73,8 @@ def unbind() -> Generator[None, None, None]: class ScopeContext(FusedContextManager[None]): + __slots__ = ("state", "scope") + def __init__(self, state: ContainerState, scope: Scope) -> None: self.state = state self.scope = scope diff --git a/di/_task.py b/di/_task.py index 3f2f2671..1b742df8 100644 --- a/di/_task.py +++ b/di/_task.py @@ -18,6 +18,8 @@ class Task(Generic[DependencyType]): + __slots__ = ("dependant", "dependencies") + def __init__( self, dependant: DependantProtocol[DependencyType], @@ -54,6 +56,8 @@ def use_value( class AsyncTask(Task[DependencyType]): + __slots__ = () + async def compute( self, state: ContainerState, @@ -89,6 +93,8 @@ async def compute( class SyncTask(Task[DependencyType]): + __slots__ = () + def compute( self, state: ContainerState, diff --git a/di/dependant.py b/di/dependant.py index 34b820b2..a7bd1aa2 100644 --- a/di/dependant.py +++ b/di/dependant.py @@ -23,7 +23,7 @@ ) -class Dependant(DependantProtocol[DependencyType], object): +class Dependant(DependantProtocol[DependencyType]): wire: bool autowire: bool diff --git a/di/types/__init__.py b/di/types/__init__.py index 7161c32c..1c3e5104 100644 --- a/di/types/__init__.py +++ b/di/types/__init__.py @@ -1,7 +1,36 @@ -from typing import AsyncContextManager, ContextManager, TypeVar +import sys +from types import TracebackType +from typing import Optional, Type, TypeVar, Union -T = TypeVar("T") +if sys.version_info >= (3, 8): + from typing import Protocol +else: + from typing_extensions import Protocol +T = TypeVar("T", covariant=True) -class FusedContextManager(AsyncContextManager[T], ContextManager[T]): - ... + +class FusedContextManager(Protocol[T]): + __slots__ = () + + def __enter__(self): + ... + + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> Union[None, bool]: + ... + + async def __aenter__(self): + ... + + async def __aexit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> Union[None, bool]: + ... diff --git a/pyproject.toml b/pyproject.toml index 01fd6d3c..bd4abb06 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "di" -version = "0.18.5" +version = "0.18.6" description = "Autowiring dependency injection" authors = ["Adrian Garcia Badaracco "] readme = "README.md"