Skip to content

Commit

Permalink
Restore the annotated dynamic deps support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Oct 7, 2024
1 parent 3ae38e6 commit 941ed70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scrapy_poet/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from weakref import WeakKeyDictionary

import andi
from andi.typeutils import issubclass_safe
from andi.typeutils import issubclass_safe, strip_annotated
from scrapy import Request, Spider
from scrapy.crawler import Crawler
from scrapy.http import Response
Expand Down Expand Up @@ -247,7 +247,7 @@ def _get_dynamic_deps_factory(
"""
ns: Dict[str, type] = {}
for type_ in dynamic_types:
if not isinstance(type_, type):
if not isinstance(strip_annotated(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
17 changes: 17 additions & 0 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,23 @@ def test_dynamic_deps_factory():
assert dd == {int: 42, Cls1: c}


@pytest.mark.skipif(
sys.version_info < (3, 9), reason="No Annotated support in Python < 3.9"
)
def test_dynamic_deps_factory_annotated():
from typing import Annotated

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


def test_dynamic_deps_factory_bad_input():
with pytest.raises(
TypeError,
Expand Down

0 comments on commit 941ed70

Please sign in to comment.