From d2da66fe15fdf2e407073d0c087f3c6e97cc10c1 Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Tue, 24 Sep 2024 17:41:29 +0500 Subject: [PATCH] Check types of the inject meta value. --- scrapy_poet/injection.py | 6 +++++- tests/test_injection.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/scrapy_poet/injection.py b/scrapy_poet/injection.py index 3414741a..7bac5b68 100644 --- a/scrapy_poet/injection.py +++ b/scrapy_poet/injection.py @@ -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) diff --git a/tests/test_injection.py b/tests/test_injection.py index fe68e5a9..90f03104 100644 --- a/tests/test_injection.py +++ b/tests/test_injection.py @@ -1,3 +1,4 @@ +import re import shutil import sys from typing import Any, Callable, Dict, Generator, Optional @@ -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 (,)"), + ): + Injector._get_dynamic_deps_factory([(int,)])