-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
core.py
147 lines (102 loc) · 2.88 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# SPDX-FileCopyrightText: 2023 Hynek Schlawack <hs@ox.cx>
#
# SPDX-License-Identifier: MIT
import contextlib
import sys
from collections.abc import AsyncGenerator, Generator
from typing import NewType, Protocol
import svcs
reg = svcs.Registry()
con = svcs.Container(reg)
reg.close()
with contextlib.closing(reg) as reg:
...
with reg as reg:
reg.register_factory(int, int)
async def func() -> None:
await reg.aclose()
await con.aclose()
async with svcs.Registry() as reg2:
reg2.register_factory(int, int)
async with svcs.Container(reg2) as con2:
a: int
b: str
c: bool
d: tuple
e: object
f: float
g: list
h: dict
i: set
j: bytes
a, b, c, d, e, f, g, h, i, j = await con2.aget(
int, str, bool, tuple, object, float, list, dict, set, bytes
)
def gen() -> Generator:
yield 42
async def async_gen() -> AsyncGenerator:
yield 42
@contextlib.asynccontextmanager
async def async_cm() -> AsyncGenerator:
yield 42
def factory_with_cleanup() -> Generator[int, None, None]:
yield 1
@contextlib.contextmanager
def factory_with_cleanup_ctx() -> Generator[int, None, None]:
yield 1
def factory_that_takes_container_by_annotation(foo: svcs.Container) -> int:
return 1
async def async_ping() -> None:
pass
reg.register_value(int, 1)
reg.register_value(int, 1, ping=lambda: None)
reg.register_value(int, 1, ping=async_ping)
reg.register_value(int, gen)
reg.register_factory(str, str)
reg.register_factory(int, factory_with_cleanup)
reg.register_factory(int, factory_with_cleanup_ctx)
reg.register_factory(int, factory_with_cleanup, ping=async_ping)
reg.register_factory(str, async_gen)
reg.register_factory(str, async_cm)
reg.register_value(str, str, ping=lambda: None)
con = svcs.Container(reg)
# Local registries
con.register_local_factory(int, factory_with_cleanup)
con.register_local_value(int, 42)
# The type checker believes whatever we tell it.
o1: object = con.get(object)
o2: int = con.get(int)
a: int
b: str
c: bool
d: tuple
e: object
f: float
g: list
h: dict
i: set
j: bytes
a, b, c, d, e, f, g, h, i, j = con.get(
int, str, bool, tuple, object, float, list, dict, set, bytes
)
class P(Protocol):
def m(self) -> None: ...
p: P = con.get_abstract(P)
con.close()
with contextlib.closing(svcs.Container(reg)) as con:
...
with svcs.Container(reg) as con:
i2: int = con.get(int)
if sys.version_info >= (3, 10):
async def ctx() -> None:
async with contextlib.aclosing(svcs.Container(reg)):
...
async with contextlib.aclosing(svcs.Registry()):
...
# Multiple factories for same type:
S1 = NewType("S1", str)
S2 = NewType("S2", str)
reg.register_value(S1, "foo")
reg.register_value(S2, "bar")
s1: str = con.get(S1)
s2: str = con.get(S2)