Skip to content

Commit af7b71a

Browse files
committed
Make inject markers for Annotations public
This is probably as close as a fix for #158 as we'll get on this end.
1 parent 0e9905e commit af7b71a

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

injector/__init__.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
9898
lock = threading.RLock()
9999

100100

101-
_inject_marker = object()
102-
_noinject_marker = object()
101+
inject_marker = object()
102+
noinject_marker = object()
103103

104104
InjectT = TypeVar('InjectT')
105-
Inject = Annotated[InjectT, _inject_marker]
105+
Inject = Annotated[InjectT, inject_marker]
106106
"""An experimental way to declare injectable dependencies utilizing a `PEP 593`_ implementation
107107
in Python 3.9 and backported to Python 3.7+ in `typing_extensions`.
108108
109-
Those two declarations are equivalent::
109+
These three declarations are equivalent::
110110
111111
@inject
112112
def fun(t: SomeType) -> None:
@@ -115,8 +115,11 @@ def fun(t: SomeType) -> None:
115115
def fun(t: Inject[SomeType]) -> None:
116116
pass
117117
118+
def fun(t: Annotated[SomeType, inject_marker]) -> None:
119+
pass
120+
118121
The advantage over using :func:`inject` is that if you have some noninjectable parameters
119-
it may be easier to spot what are they. Those two are equivalent::
122+
it may be easier to spot what are they. These two are equivalent::
120123
121124
@inject
122125
@noninjectable('s')
@@ -142,7 +145,7 @@ def fun(t: Inject[SomeType], s: SomeOtherType) -> None:
142145
.. _typing_extensions: https://pypi.org/project/typing-extensions/
143146
"""
144147

145-
NoInject = Annotated[InjectT, _noinject_marker]
148+
NoInject = Annotated[InjectT, noinject_marker]
146149
"""An experimental way to declare noninjectable dependencies utilizing a `PEP 593`_ implementation
147150
in Python 3.9 and backported to Python 3.7+ in `typing_extensions`.
148151
@@ -154,7 +157,7 @@ def fun(t: Inject[SomeType], s: SomeOtherType) -> None:
154157
* The declaration may be relatively distance in space from the actual parameter declaration, thus
155158
hindering readability
156159
157-
`NoInject` solves both of those concerns, for example (those two declarations are equivalent)::
160+
`NoInject` solves both of those concerns, for example (these three declarations are equivalent)::
158161
159162
@inject
160163
@noninjectable('b')
@@ -165,6 +168,10 @@ def fun(a: TypeA, b: TypeB) -> None:
165168
def fun(a: TypeA, b: NoInject[TypeB]) -> None:
166169
pass
167170
171+
@inject
172+
def fun(a: TypeA, b: Annotated[TypeB, noinject_marker]) -> None:
173+
pass
174+
168175
.. seealso::
169176
170177
Function :func:`get_bindings`
@@ -1166,7 +1173,7 @@ def get_bindings(callable: Callable) -> Dict[str, type]:
11661173
if not hasattr(callable, '__bindings__'):
11671174
type_hints = get_type_hints(callable, include_extras=True)
11681175
has_injectable_parameters = any(
1169-
_is_specialization(v, Annotated) and _inject_marker in v.__metadata__ for v in type_hints.values()
1176+
_is_specialization(v, Annotated) and inject_marker in v.__metadata__ for v in type_hints.values()
11701177
)
11711178

11721179
if not has_injectable_parameters:
@@ -1244,7 +1251,7 @@ def _is_new_union_type(instance: Any) -> bool:
12441251
else:
12451252
metadata = tuple()
12461253

1247-
if only_explicit_bindings and _inject_marker not in metadata or _noinject_marker in metadata:
1254+
if only_explicit_bindings and inject_marker not in metadata or noinject_marker in metadata:
12481255
del bindings[k]
12491256
elif _is_specialization(v, Union) or _is_new_union_type(v):
12501257
# We don't treat Optional parameters in any special way at the moment.
@@ -1264,8 +1271,8 @@ def _is_new_union_type(instance: Any) -> bool:
12641271
}
12651272
if (
12661273
only_explicit_bindings
1267-
and _inject_marker not in union_metadata
1268-
or _noinject_marker in union_metadata
1274+
and inject_marker not in union_metadata
1275+
or noinject_marker in union_metadata
12691276
):
12701277
del bindings[k]
12711278
else:

0 commit comments

Comments
 (0)