Scrapy, a fast high-level web crawling & scraping framework for Python.
Raise on unresolvable component priority dictionary keys (#8159)
Adrian42030b24da52
diff --git a/docs/topics/settings.rst b/docs/topics/settings.rstindex 1af07eacc..dbc60c952 100644--- a/docs/topics/settings.rst+++ b/docs/topics/settings.rst@@ -262,6 +262,13 @@ example:A component can be specified either as a class object or through an importpath.+A key that cannot be resolved into a component, such as the import path of a+component that no longer exists, raises an exception, even if its priority is+:data:`None`.++.. versionchanged:: VERSION+ Unresolvable keys used to be silently ignored in some cases.+.. warning:: Component priority dictionaries are regular :class:`dict` objects.Be careful not to define the same component more than once, e.g. withdifferent import path strings or defining both an import path and adiff --git a/scrapy/settings/__init__.py b/scrapy/settings/__init__.pyindex 5c5edf101..6870ba374 100644--- a/scrapy/settings/__init__.py+++ b/scrapy/settings/__init__.py@@ -363,17 +363,18 @@ class BaseSettings(MutableMapping[str, Any]):f"be kept.")- def normalize_key(key: Any) -> Any:+ def normalize_key(key: Any) -> str:try:loaded_key = load_object(key)- except (NameError, TypeError, ValueError):- loaded_key = key- else:- import_path = global_object_name(loaded_key)- normalized_keys[import_path] = key- key = import_path+ except (ImportError, NameError, TypeError, ValueError) as exception:+ raise ValueError(+ f"Could not load {key!r}, a key of the {name} setting: "+ f"{exception}. Fix its import path or remove the entry."+ ) from exception+ import_path = global_object_name(loaded_key)+ normalized_keys[import_path] = keytrack_loaded_key(loaded_key)- return key+ return import_pathdef restore_key(k: str) -> Any:return normalized_keys.get(k, k)diff --git a/tests/test_settings/__init__.py b/tests/test_settings/__init__.pyindex a33d58222..18940dcd2 100644--- a/tests/test_settings/__init__.py+++ b/tests/test_settings/__init__.py@@ -2,6 +2,8 @@# (too many false positives)import logging+import re+from typing import Anyfrom unittest import mockimport pytest@@ -422,29 +424,32 @@ class TestBaseSettings:"csv.gz": "bar",}+ @pytest.mark.parametrize("priority", [1, None])@pytest.mark.parametrize(("key", "exception"),[pytest.param(1, TypeError, id="type-error"),pytest.param("foo", ValueError, id="value-error"),pytest.param("csv.gz", NameError, id="name-error"),+ pytest.param(+ "nonexistent.module.Component", ImportError, id="import-error"+ ),],)- def test_get_component_priority_dict_with_base_handles_load_object_exceptions(- self, key, exception- ):+ def test_get_component_priority_dict_with_base_unresolvable_key(+ self, key: Any, exception: type[Exception], priority: int | None+ ) -> None:with pytest.raises(exception):load_object(key)settings = BaseSettings({- "FOO": BaseSettings({key: 1}),+ "FOO_BASE": BaseSettings({"csv.excel": 1}),+ "FOO": BaseSettings({key: priority}),})- value = settings.get_component_priority_dict_with_base("FOO")-- assert isinstance(value, BaseSettings)- assert dict(value) == {key: 1}+ with pytest.raises(ValueError, match=re.escape(f"{key!r}, a key of the FOO")):+ settings.get_component_priority_dict_with_base("FOO")def test_get_component_priority_dict_with_base_override_none_by_type(self):settings = BaseSettings()