Skip to content

Commit 127e4d2

Browse files
Add type stubs for punq (#15274)
1 parent e872935 commit 127e4d2

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"stubs/protobuf",
7575
"stubs/psutil/psutil/__init__.pyi",
7676
"stubs/psycopg2",
77+
"stubs/punq",
7778
"stubs/pyasn1",
7879
"stubs/pycurl",
7980
"stubs/Pygments",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
punq._Empty.__init__

stubs/punq/METADATA.toml

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

stubs/punq/punq/__init__.pyi

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from collections.abc import Callable
2+
from enum import Enum
3+
from typing import Any, Final, Generic, NamedTuple, NewType, TypeVar, overload
4+
5+
__version__: str
6+
7+
class MissingDependencyException(Exception): ...
8+
class MissingDependencyError(MissingDependencyException): ...
9+
class InvalidRegistrationException(Exception): ...
10+
class InvalidRegistrationError(InvalidRegistrationException): ...
11+
class InvalidForwardReferenceException(Exception): ...
12+
class InvalidForwardReferenceError(InvalidForwardReferenceException): ...
13+
14+
class Scope(Enum):
15+
transient = 0
16+
singleton = 1
17+
18+
_T = TypeVar("_T", default=Any)
19+
20+
class _Registration(NamedTuple, Generic[_T]):
21+
service: type[_T] | str
22+
scope: Scope
23+
builder: Callable[..., _T]
24+
needs: dict[str, Any] # the type hints of the builder's parameters
25+
args: dict[str, Any] # passed to builder at instantiation time
26+
27+
_Empty = NewType("_Empty", object) # a class at runtime
28+
empty: Final[_Empty]
29+
30+
class _Registry:
31+
def register_service_and_impl(
32+
self,
33+
service: type[_T] | str,
34+
scope: Scope,
35+
impl: type[_T],
36+
resolve_args: dict[str, Any], # forwarded to _Registration.builder
37+
) -> None: ...
38+
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: ...
40+
def build_context(self, key: type | str, existing: _ResolutionContext | None = None) -> _ResolutionContext: ...
41+
def register(
42+
self,
43+
service: type[_T] | str,
44+
factory: Callable[..., _T] | _Empty = ...,
45+
instance: _T | _Empty = ...,
46+
scope: Scope = Scope.transient,
47+
**kwargs: Any, # forwarded to _Registration.builder
48+
) -> None: ...
49+
def __getitem__(self, service: type[_T] | str) -> list[_Registration[_T]]: ...
50+
51+
class _ResolutionTarget(Generic[_T]):
52+
service: type[_T] | str
53+
impls: list[_Registration[_T]]
54+
def __init__(self, key: type[_T] | str, impls: list[_Registration[_T]]) -> None: ...
55+
def is_generic_list(self) -> bool: ...
56+
@property
57+
def generic_parameter(self) -> Any: ... # returns the first annotated generic parameter of the service
58+
def next_impl(self) -> _Registration[_T]: ...
59+
60+
class _ResolutionContext:
61+
targets: dict[type | str, _ResolutionTarget[Any]]
62+
cache: dict[type | str, Any] # resolved objects during this resolution
63+
service: type | str
64+
def __init__(self, key: type | str, impls: list[_Registration[Any]]) -> None: ...
65+
def target(self, key: type[_T] | str) -> _ResolutionTarget[_T]: ...
66+
def has_cached(self, key: type | str) -> bool: ...
67+
def __getitem__(self, key: type[_T] | str) -> _T: ...
68+
def __setitem__(self, key: type[_T] | str, value: _T) -> None: ...
69+
def all_registrations(self, service: type[_T] | str) -> list[_Registration[_T]]: ...
70+
71+
class Container:
72+
registrations: _Registry
73+
def __init__(self) -> None: ...
74+
# all kwargs are forwarded to _Registration.builder
75+
@overload
76+
def register(self, service: type[_T] | str, *, instance: _T, **kwargs: Any) -> Container: ...
77+
@overload
78+
def register(
79+
self, service: type[_T] | str, factory: Callable[..., _T] | _Empty = ..., *, scope: Scope = Scope.transient, **kwargs: Any
80+
) -> Container: ...
81+
@overload
82+
def register(
83+
self,
84+
service: type[_T] | str,
85+
factory: Callable[..., _T] | _Empty = ...,
86+
instance: _T | _Empty = ...,
87+
scope: Scope = Scope.transient,
88+
**kwargs: Any,
89+
): ...
90+
def resolve_all(self, service: type[_T] | str, **kwargs: Any) -> list[_T]: ...
91+
def resolve(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ...
92+
def instantiate(self, service_key: type[_T] | str, **kwargs: Any) -> _T: ...

0 commit comments

Comments
 (0)