Skip to content

Commit

Permalink
A better fix with 3.9 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Oct 7, 2024
1 parent 941ed70 commit f6c644b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion scrapy_poet/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ def _get_dynamic_deps_factory(
"""
ns: Dict[str, type] = {}
for type_ in dynamic_types:
if not isinstance(strip_annotated(type_), type):
type_ = strip_annotated(type_)
if not isinstance(type_, type):
raise TypeError(f"Expected a dynamic dependency type, got {type_!r}")
ns[type_.__name__] = type_
txt = Injector._get_dynamic_deps_factory_text(ns.keys())
Expand Down
13 changes: 8 additions & 5 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,15 +995,18 @@ def test_dynamic_deps_factory():
def test_dynamic_deps_factory_annotated():
from typing import Annotated

fn = Injector._get_dynamic_deps_factory([Annotated[Cls1, 42]])
fn = Injector._get_dynamic_deps_factory(
[Annotated[Cls1, 42], Annotated[Cls2, "foo"]]
)
args = andi.inspect(fn)
# the arg name needs to be fixed separately
assert args == {
"Annotated_arg": [Annotated[Cls1, 42]],
"Cls1_arg": [Annotated[Cls1, 42]],
"Cls2_arg": [Annotated[Cls2, "foo"]],
}
c1 = Cls1()
dd = fn(Annotated_arg=c1)
assert dd == {Annotated[Cls1, 42]: c1}
c2 = Cls2()
dd = fn(Cls1_arg=c1, Cls2_arg=c2)
assert dd == {Annotated[Cls1, 42]: c1, Annotated[Cls2, "foo"]: c2}


def test_dynamic_deps_factory_bad_input():
Expand Down

0 comments on commit f6c644b

Please sign in to comment.