diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 233d97b53514..8f32cb3e46fa 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -78,6 +78,7 @@ "stubs/protobuf", "stubs/psutil/psutil/__init__.pyi", "stubs/psycopg2", + "stubs/punq", "stubs/pyasn1", "stubs/Pygments", "stubs/PyMySQL", diff --git a/stubs/punq/@tests/stubtest_allowlist.txt b/stubs/punq/@tests/stubtest_allowlist.txt index e2c11ed54e4d..9a0d8e0f9e3a 100644 --- a/stubs/punq/@tests/stubtest_allowlist.txt +++ b/stubs/punq/@tests/stubtest_allowlist.txt @@ -1 +1,2 @@ - punq._Empty.__init__ +punq._Empty.__init__ +punq._Registration.__class_getitem__ diff --git a/stubs/punq/METADATA.toml b/stubs/punq/METADATA.toml index f29f602eea39..0c3e8e97fbb8 100644 --- a/stubs/punq/METADATA.toml +++ b/stubs/punq/METADATA.toml @@ -1,4 +1,2 @@ -version = "0.7.*" +version = "0.8.*" upstream-repository = "https://github.com/bobthemighty/punq" - -[tool.stubtest] diff --git a/stubs/punq/punq/__init__.pyi b/stubs/punq/punq/__init__.pyi index c6b2d50fac73..1fb20df72028 100644 --- a/stubs/punq/punq/__init__.pyi +++ b/stubs/punq/punq/__init__.pyi @@ -1,42 +1,78 @@ +from _typeshed import Incomplete +from collections import defaultdict from collections.abc import Callable -from enum import Enum +from enum import Enum, unique from typing import Any, Final, Generic, NamedTuple, NewType, TypeVar, overload +from typing_extensions import Self, deprecated -__version__: str +_T = TypeVar("_T", default=Any) + +__version__: Final[str] +@deprecated("Deprecated alias for `MissingDependencyError`.") class MissingDependencyException(Exception): ... + class MissingDependencyError(MissingDependencyException): ... + +@deprecated("Deprecated alias for `InvalidRegistrationError`.") class InvalidRegistrationException(Exception): ... + class InvalidRegistrationError(InvalidRegistrationException): ... + +class InvalidFactoryError(InvalidRegistrationError): + def __init__(self, service, factory) -> None: ... + +class InvalidSelfRegistrationError(InvalidRegistrationError): + def __init__(self, service) -> None: ... + +@deprecated("Deprecated alias for `InvalidForwardReferenceError`.") class InvalidForwardReferenceException(Exception): ... + class InvalidForwardReferenceError(InvalidForwardReferenceException): ... +# TODO: Make this class Generic +class RegistrationScope: + parent: RegistrationScope | None + entries: defaultdict[Incomplete, list[Incomplete]] + def __init__(self, parent: RegistrationScope | None = None) -> None: ... + def child(self) -> Self: ... + def append(self, key, value) -> None: ... + def get(self, key) -> list[Incomplete]: ... + +@unique class Scope(Enum): transient = 0 singleton = 1 -_T = TypeVar("_T", default=Any) - class _Registration(NamedTuple, Generic[_T]): service: type[_T] | str scope: Scope builder: Callable[..., _T] needs: dict[str, Any] # the type hints of the builder's parameters args: dict[str, Any] # passed to builder at instantiation time + cache: bool _Empty = NewType("_Empty", object) # a class at runtime empty: Final[_Empty] class _Registry: + def __init__(self, parent: _Registry | None = None) -> None: ... def register_service_and_impl( self, service: type[_T] | str, scope: Scope, impl: type[_T], resolve_args: dict[str, Any], # forwarded to _Registration.builder + cache: bool = True, ) -> None: ... def register_service_and_instance(self, service: type[_T] | str, instance: _T) -> None: ... - def register_concrete_service(self, service: type | str, scope: Scope) -> None: ... + def register_concrete_service( + self, + service: type | str, + scope: Scope, + resolve_args: dict[str, Any] | None = None, # forwarded to _Registration.builder + cache: bool = True, + ) -> None: ... def build_context(self, key: type | str, existing: _ResolutionContext | None = None) -> _ResolutionContext: ... def register( self, @@ -44,9 +80,9 @@ class _Registry: factory: Callable[..., _T] | _Empty = ..., instance: _T | _Empty = ..., scope: Scope = Scope.transient, + cache: bool = True, **kwargs: Any, # forwarded to _Registration.builder ) -> None: ... - def __getitem__(self, service: type[_T] | str) -> list[_Registration[_T]]: ... class _ResolutionTarget(Generic[_T]): service: type[_T] | str @@ -55,7 +91,7 @@ class _ResolutionTarget(Generic[_T]): def is_generic_list(self) -> bool: ... @property def generic_parameter(self) -> Any: ... # returns the first annotated generic parameter of the service - def next_impl(self) -> _Registration[_T]: ... + def next_impl(self) -> _Registration[_T] | None: ... class _ResolutionContext: targets: dict[type | str, _ResolutionTarget[Any]] @@ -70,15 +106,21 @@ class _ResolutionContext: class Container: registrations: _Registry - def __init__(self) -> None: ... + def __init__(self, registrations: _Registry | None = None, auto_register: bool = False) -> None: ... # all kwargs are forwarded to _Registration.builder @overload - def register(self, service: type[_T] | str, *, instance: _T, **kwargs: Any) -> Container: ... + def register(self, service: type[_T] | str, *, instance: _T, cache: bool = True, **kwargs: Any) -> Self: ... @overload def register( - self, service: type[_T] | str, factory: Callable[..., _T] | _Empty = ..., *, scope: Scope = Scope.transient, **kwargs: Any - ) -> Container: ... + self, + service: type[_T] | str, + factory: Callable[..., _T] | _Empty = ..., + *, + scope: Scope = Scope.transient, + cache: bool = True, + **kwargs: Any, + ) -> Self: ... @overload def register( self, @@ -86,9 +128,11 @@ class Container: factory: Callable[..., _T] | _Empty = ..., instance: _T | _Empty = ..., scope: Scope = Scope.transient, + cache: bool = True, **kwargs: Any, - ) -> Container: ... + ) -> Self: ... def resolve_all(self, service: type[_T] | str, **kwargs: Any) -> list[_T]: ... def resolve(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ... def instantiate(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ... + def child(self) -> Self: ...