Skip to content

Commit

Permalink
Merge pull request #207 from scrapinghub/dynamic-check
Browse files Browse the repository at this point in the history
Check types of the inject meta value.
  • Loading branch information
wRAR authored Sep 26, 2024
2 parents e7dab40 + d2da66f commit 3ae38e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion scrapy_poet/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ def _get_dynamic_deps_factory(
corresponding args. It has correct type hints so that it can be used as
an ``andi`` custom builder.
"""
ns = {type_.__name__: type_ for type_ in dynamic_types}
ns: Dict[str, type] = {}
for type_ in dynamic_types:
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())
exec(txt, globals(), ns)
return ns["__create_fn__"](*dynamic_types)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import shutil
import sys
from typing import Any, Callable, Dict, Generator, Optional
Expand Down Expand Up @@ -986,3 +987,11 @@ def test_dynamic_deps_factory():
c = Cls1()
dd = fn(int_arg=42, Cls1_arg=c)
assert dd == {int: 42, Cls1: c}


def test_dynamic_deps_factory_bad_input():
with pytest.raises(
TypeError,
match=re.escape(r"Expected a dynamic dependency type, got (<class 'int'>,)"),
):
Injector._get_dynamic_deps_factory([(int,)])

0 comments on commit 3ae38e6

Please sign in to comment.