Skip to content

Commit

Permalink
Add the full annotated dynamic deps test with the current behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Oct 7, 2024
1 parent f6c644b commit e9f8a43
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scrapy_poet/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _get_dynamic_deps_factory(
"""
ns: Dict[str, type] = {}
for type_ in dynamic_types:
type_ = strip_annotated(type_)
type_ = cast(type, strip_annotated(type_))
if not isinstance(type_, type):
raise TypeError(f"Expected a dynamic dependency type, got {type_!r}")
ns[type_.__name__] = type_
Expand Down
33 changes: 33 additions & 0 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,39 @@ def callback(dd: DynamicDeps):
instances = yield from injector.build_instances(request, response, plan)
assert set(instances) == {TestItemPage, TestItem, DynamicDeps}

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

def callback(dd: DynamicDeps):
pass

provider = get_provider({Cls1, Cls2})
injector = get_injector_for_testing({provider: 1})

expected_instances = {
DynamicDeps: DynamicDeps(
{Annotated[Cls1, 42]: Cls1(), Annotated[Cls2, "foo"]: Cls2()}
),
Annotated[Cls1, 42]: Cls1(),
Annotated[Cls2, "foo"]: Cls2(),
}
expected_kwargs = {
"dd": DynamicDeps(
{Annotated[Cls1, 42]: Cls1(), Annotated[Cls2, "foo"]: Cls2()}
),
}
yield self._assert_instances(
injector,
callback,
expected_instances,
expected_kwargs,
reqmeta={"inject": [Annotated[Cls1, 42], Annotated[Cls2, "foo"]]},
)


class Html(Injectable):
url = "http://example.com"
Expand Down

0 comments on commit e9f8a43

Please sign in to comment.