Skip to content

Commit 5866e12

Browse files
AvlWx2014sheinbergon
authored andcommitted
_infer_injected_bindings: Don't strip NoneType from function bindings
1 parent 1d58fd7 commit 5866e12

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

injector/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,9 +1263,8 @@ def _recreate_annotated_origin(annotated_type: Any) -> Any:
12631263
if only_explicit_bindings and _inject_marker not in metadata or _noinject_marker in metadata:
12641264
del bindings[k]
12651265
elif _is_specialization(v, Union) or _is_new_union_type(v):
1266-
# We don't treat Optional parameters in any special way at the moment.
12671266
union_members = v.__args__
1268-
new_members = tuple(set(union_members) - {type(None)})
1267+
new_members = tuple(set(union_members))
12691268
# mypy stared complaining about this line for some reason:
12701269
# error: Variable "new_members" is not valid as a type
12711270
new_union = Union[new_members] # type: ignore

injector_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,8 +1761,7 @@ def function(a: Inject[Inject[int]]) -> None:
17611761
assert get_bindings(function) == {'a': int}
17621762

17631763

1764-
# This will not inject `None` even if there is a provider configured to provide
1765-
# str | None elsewhere in the graph because `None` is stripped in
1764+
# This should correctly resolve str | None
17661765
@inject
17671766
def function12(a: str | None):
17681767
pass
@@ -1777,7 +1776,7 @@ def test_get_bindings_for_pep_604():
17771776
def function1(a: int | None) -> None:
17781777
pass
17791778

1780-
assert get_bindings(function1) == {'a': int}
1779+
assert get_bindings(function1) == {'a': Union[int, None]}
17811780

17821781
@inject
17831782
def function1(a: int | str) -> None:

0 commit comments

Comments
 (0)