Skip to content

Commit f5aa7d5

Browse files
authored
[punq] Update to 0.8.* (#16037)
1 parent c4eaaca commit f5aa7d5

4 files changed

Lines changed: 60 additions & 16 deletions

File tree

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"stubs/protobuf",
7979
"stubs/psutil/psutil/__init__.pyi",
8080
"stubs/psycopg2",
81+
"stubs/punq",
8182
"stubs/pyasn1",
8283
"stubs/Pygments",
8384
"stubs/PyMySQL",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
punq._Empty.__init__
1+
punq._Empty.__init__
2+
punq._Registration.__class_getitem__

stubs/punq/METADATA.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
version = "0.7.*"
1+
version = "0.8.*"
22
upstream-repository = "https://github.com/bobthemighty/punq"
3-
4-
[tool.stubtest]

stubs/punq/punq/__init__.pyi

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,88 @@
1+
from _typeshed import Incomplete
2+
from collections import defaultdict
13
from collections.abc import Callable
2-
from enum import Enum
4+
from enum import Enum, unique
35
from typing import Any, Final, Generic, NamedTuple, NewType, TypeVar, overload
6+
from typing_extensions import Self, deprecated
47

5-
__version__: str
8+
_T = TypeVar("_T", default=Any)
9+
10+
__version__: Final[str]
611

12+
@deprecated("Deprecated alias for `MissingDependencyError`.")
713
class MissingDependencyException(Exception): ...
14+
815
class MissingDependencyError(MissingDependencyException): ...
16+
17+
@deprecated("Deprecated alias for `InvalidRegistrationError`.")
918
class InvalidRegistrationException(Exception): ...
19+
1020
class InvalidRegistrationError(InvalidRegistrationException): ...
21+
22+
class InvalidFactoryError(InvalidRegistrationError):
23+
def __init__(self, service, factory) -> None: ...
24+
25+
class InvalidSelfRegistrationError(InvalidRegistrationError):
26+
def __init__(self, service) -> None: ...
27+
28+
@deprecated("Deprecated alias for `InvalidForwardReferenceError`.")
1129
class InvalidForwardReferenceException(Exception): ...
30+
1231
class InvalidForwardReferenceError(InvalidForwardReferenceException): ...
1332

33+
# TODO: Make this class Generic
34+
class RegistrationScope:
35+
parent: RegistrationScope | None
36+
entries: defaultdict[Incomplete, list[Incomplete]]
37+
def __init__(self, parent: RegistrationScope | None = None) -> None: ...
38+
def child(self) -> Self: ...
39+
def append(self, key, value) -> None: ...
40+
def get(self, key) -> list[Incomplete]: ...
41+
42+
@unique
1443
class Scope(Enum):
1544
transient = 0
1645
singleton = 1
1746

18-
_T = TypeVar("_T", default=Any)
19-
2047
class _Registration(NamedTuple, Generic[_T]):
2148
service: type[_T] | str
2249
scope: Scope
2350
builder: Callable[..., _T]
2451
needs: dict[str, Any] # the type hints of the builder's parameters
2552
args: dict[str, Any] # passed to builder at instantiation time
53+
cache: bool
2654

2755
_Empty = NewType("_Empty", object) # a class at runtime
2856
empty: Final[_Empty]
2957

3058
class _Registry:
59+
def __init__(self, parent: _Registry | None = None) -> None: ...
3160
def register_service_and_impl(
3261
self,
3362
service: type[_T] | str,
3463
scope: Scope,
3564
impl: type[_T],
3665
resolve_args: dict[str, Any], # forwarded to _Registration.builder
66+
cache: bool = True,
3767
) -> None: ...
3868
def register_service_and_instance(self, service: type[_T] | str, instance: _T) -> None: ...
39-
def register_concrete_service(self, service: type | str, scope: Scope) -> None: ...
69+
def register_concrete_service(
70+
self,
71+
service: type | str,
72+
scope: Scope,
73+
resolve_args: dict[str, Any] | None = None, # forwarded to _Registration.builder
74+
cache: bool = True,
75+
) -> None: ...
4076
def build_context(self, key: type | str, existing: _ResolutionContext | None = None) -> _ResolutionContext: ...
4177
def register(
4278
self,
4379
service: type[_T] | str,
4480
factory: Callable[..., _T] | _Empty = ...,
4581
instance: _T | _Empty = ...,
4682
scope: Scope = Scope.transient,
83+
cache: bool = True,
4784
**kwargs: Any, # forwarded to _Registration.builder
4885
) -> None: ...
49-
def __getitem__(self, service: type[_T] | str) -> list[_Registration[_T]]: ...
5086

5187
class _ResolutionTarget(Generic[_T]):
5288
service: type[_T] | str
@@ -55,7 +91,7 @@ class _ResolutionTarget(Generic[_T]):
5591
def is_generic_list(self) -> bool: ...
5692
@property
5793
def generic_parameter(self) -> Any: ... # returns the first annotated generic parameter of the service
58-
def next_impl(self) -> _Registration[_T]: ...
94+
def next_impl(self) -> _Registration[_T] | None: ...
5995

6096
class _ResolutionContext:
6197
targets: dict[type | str, _ResolutionTarget[Any]]
@@ -70,25 +106,33 @@ class _ResolutionContext:
70106

71107
class Container:
72108
registrations: _Registry
73-
def __init__(self) -> None: ...
109+
def __init__(self, registrations: _Registry | None = None, auto_register: bool = False) -> None: ...
74110

75111
# all kwargs are forwarded to _Registration.builder
76112
@overload
77-
def register(self, service: type[_T] | str, *, instance: _T, **kwargs: Any) -> Container: ...
113+
def register(self, service: type[_T] | str, *, instance: _T, cache: bool = True, **kwargs: Any) -> Self: ...
78114
@overload
79115
def register(
80-
self, service: type[_T] | str, factory: Callable[..., _T] | _Empty = ..., *, scope: Scope = Scope.transient, **kwargs: Any
81-
) -> Container: ...
116+
self,
117+
service: type[_T] | str,
118+
factory: Callable[..., _T] | _Empty = ...,
119+
*,
120+
scope: Scope = Scope.transient,
121+
cache: bool = True,
122+
**kwargs: Any,
123+
) -> Self: ...
82124
@overload
83125
def register(
84126
self,
85127
service: type[_T] | str,
86128
factory: Callable[..., _T] | _Empty = ...,
87129
instance: _T | _Empty = ...,
88130
scope: Scope = Scope.transient,
131+
cache: bool = True,
89132
**kwargs: Any,
90-
) -> Container: ...
133+
) -> Self: ...
91134

92135
def resolve_all(self, service: type[_T] | str, **kwargs: Any) -> list[_T]: ...
93136
def resolve(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ...
94137
def instantiate(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ...
138+
def child(self) -> Self: ...

0 commit comments

Comments
 (0)