Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check types of the inject meta value. #207

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,)])
Loading