Backtestsscrapyrun-20261025-211112#7759

Issue #7759 Merged fix PR #7763 Run 25 Oct 2026 · run-20261025-211112 scrapy/scrapy

BaseSettings.update() does not accept iterable key-value pairs

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches make the identical functional change: extend the _SettingsInput alias with Iterable[tuple[str, Any]] and branch on hasattr(values, 'items') so non-mapping iterables are consumed directly as pairs. Both preserve the BaseSettings per-key-priority path and the JSON-string path, and the independent tests confirm both resolve every scenario (lists, tuples of two-item lists, generators, repeated keys, priority handling, Settings subclass). So correctness is a wash and the difference lies in tests and craft.

Tests: Patch A is noticeably stronger. It un-xfails the existing test and makes it assert both an overwrite and a new key with default priority, adds a priority test using a plain iterator (not just a list) that checks a higher-priority existing value is not overridden, and adds tests for setdict() and the BaseSettings/Settings constructors taking iterables, which are real entry points that delegate to update() and are now typed as accepting iterables. Patch B un-xfails the single test and adds a value + priority assertion, which is adequate but minimal and only exercises a list.

Craft: Patch B's single-loop structure with an `items` variable is slightly tidier than A's duplicated loop body, and B correctly narrows the stale 'doesn't support all inputs' comment to the remaining kwargs gap, whereas A leaves that comment unchanged (still technically true, just less precise). On the other hand, A's docstring explicitly explains the new iterable form and references dict.update, while B only tweaks the :type: line. These craft points roughly cancel.

Scope is equivalent for both. Given tests rank above craft and A's tests cover more of the surface the type change actually opens up, A is the better patch by a modest margin.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

13m 59s

Wall-clock time in the container

Full test suite

No new failures

303 passed · 66 skipped · 376 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

45vs 25 merged

Changed lines, ours vs merged

BaseSettings inherits from MutableMapping, but BaseSettings.update() does not accept iterable key-value pairs like dict.update() does.

Example:

from scrapy.settings import BaseSettings

settings = BaseSettings({"KEY": 0})
settings.update([("KEY", 1)])

This raises:

AttributeError: 'list' object has no attribute 'items'

There is already an xfailed test for this in tests/test_settings/__init__.py::TestBaseSettings::test_update_iterable.

The two patches

Our patch+37 −8
scrapy/settings/__init__.pyindex 27b0f1187..b8b8135d4 100644@@ -25,7 +25,9 @@ if TYPE_CHECKING:     # typing.Self requires Python 3.11     from typing_extensions import Self -    _SettingsInput: TypeAlias = SupportsItems[str, Any] | str | None+    _SettingsInput: TypeAlias = (+        SupportsItems[str, Any] | Iterable[tuple[str, Any]] | str | None+    )   SETTINGS_PRIORITIES: dict[str, int] = {@@ -569,6 +571,9 @@ class BaseSettings(MutableMapping[str, Any]):         :meth:`~scrapy.settings.BaseSettings.set` for every item of ``values``         with the provided ``priority``. +        ``values`` can be a mapping or, like :meth:`dict.update`, an iterable+        of key/value pairs.+         If ``values`` is a string, it is assumed to be JSON-encoded and parsed         into a dict with ``json.loads()`` first. If it is a         :class:`~scrapy.settings.BaseSettings` instance, the per-key priorities@@ -577,7 +582,8 @@ class BaseSettings(MutableMapping[str, Any]):         command.          :param values: the settings names and values-        :type values: dict or string or :class:`~scrapy.settings.BaseSettings`+        :type values: dict or iterable of key/value pairs or string or+            :class:`~scrapy.settings.BaseSettings`          :param priority: the priority of the settings. Should be a key of             :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer@@ -590,9 +596,12 @@ class BaseSettings(MutableMapping[str, Any]):             if isinstance(values, BaseSettings):                 for name, value in values.items():                     self.set(name, value, cast("int", values.getpriority(name)))-            else:+            elif hasattr(values, "items"):                 for name, value in values.items():                     self.set(name, value, priority)+            else:+                for name, value in cast("Iterable[tuple[str, Any]]", values):+                    self.set(name, value, priority)      def delete(self, name: str, priority: int | str = "project") -> None:         if name not in self:tests/test_settings/__init__.pyindex 3282b0591..bcba36495 100644@@ -221,13 +221,33 @@ class TestBaseSettings:         settings = BaseSettings({"key": 0})         settings.update(key=1)  # pylint: disable=unexpected-keyword-arg -    @pytest.mark.xfail(-        raises=AttributeError,-        reason="BaseSettings.update doesn't support iterable input",-    )     def test_update_iterable(self):         settings = BaseSettings({"key": 0})-        settings.update([("key", 1)])+        settings.update([("key", 1), ("newkey", 2)])+        assert settings["key"] == 1+        assert settings["newkey"] == 2+        assert settings.getpriority("newkey") == SETTINGS_PRIORITIES["project"]++    def test_update_iterable_priority(self):+        settings = BaseSettings({"key": 0}, priority="cmdline")+        settings.update(iter([("key", 1), ("newkey", 2)]), priority="spider")+        assert settings["key"] == 0+        assert settings["newkey"] == 2+        assert settings.getpriority("newkey") == SETTINGS_PRIORITIES["spider"]++    def test_setdict_iterable(self):+        settings = BaseSettings()+        settings.setdict([("key", 1)], priority="spider")+        assert settings["key"] == 1+        assert settings.getpriority("key") == SETTINGS_PRIORITIES["spider"]++    def test_init_iterable(self):+        settings = BaseSettings([("key", 1)], priority="spider")+        assert settings["key"] == 1+        assert settings.getpriority("key") == SETTINGS_PRIORITIES["spider"]+        settings = Settings([("key", 1)])+        assert settings["key"] == 1+        assert settings.getpriority("key") == SETTINGS_PRIORITIES["project"]      def test_update_jsonstring(self):         settings = BaseSettings({"number": 0, "dict": BaseSettings({"key": "val"})}) 
Merged fix · PR #7763+15 −10
scrapy/settings/__init__.pyindex 27b0f1187..5c5edf101 100644@@ -25,7 +25,9 @@ if TYPE_CHECKING:     # typing.Self requires Python 3.11     from typing_extensions import Self -    _SettingsInput: TypeAlias = SupportsItems[str, Any] | str | None+    _SettingsInput: TypeAlias = (+        SupportsItems[str, Any] | Iterable[tuple[str, Any]] | str | None+    )   SETTINGS_PRIORITIES: dict[str, int] = {@@ -560,7 +562,7 @@ class BaseSettings(MutableMapping[str, Any]):             if key.isupper():                 self.set(key, getattr(module, key), priority) -    # BaseSettings.update() doesn't support all inputs that MutableMapping.update() supports+    # BaseSettings.update() doesn't support kwargs input like MutableMapping.update().     def update(self, values: _SettingsInput, priority: int | str = "project") -> None:  # type: ignore[override]         """         Store key/value pairs with a given priority.@@ -577,7 +579,7 @@ class BaseSettings(MutableMapping[str, Any]):         command.          :param values: the settings names and values-        :type values: dict or string or :class:`~scrapy.settings.BaseSettings`+        :type values: dict, iterable, string or :class:`~scrapy.settings.BaseSettings`          :param priority: the priority of the settings. Should be a key of             :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer@@ -591,7 +593,12 @@ class BaseSettings(MutableMapping[str, Any]):                 for name, value in values.items():                     self.set(name, value, cast("int", values.getpriority(name)))             else:-                for name, value in values.items():+                items: Iterable[tuple[str, Any]]+                if hasattr(values, "items"):+                    items = cast("SupportsItems[str, Any]", values).items()+                else:+                    items = values+                for name, value in items:                     self.set(name, value, priority)      def delete(self, name: str, priority: int | str = "project") -> None:tests/test_settings/__init__.pyindex 3282b0591..97c095ae0 100644@@ -221,13 +221,11 @@ class TestBaseSettings:         settings = BaseSettings({"key": 0})         settings.update(key=1)  # pylint: disable=unexpected-keyword-arg -    @pytest.mark.xfail(-        raises=AttributeError,-        reason="BaseSettings.update doesn't support iterable input",-    )     def test_update_iterable(self):-        settings = BaseSettings({"key": 0})-        settings.update([("key", 1)])+        settings = BaseSettings({"key": 0}, priority=0)+        settings.update([("key", 1)], priority=10)+        assert settings["key"] == 1+        assert settings.getpriority("key") == 10      def test_update_jsonstring(self):         settings = BaseSettings({"number": 0, "dict": BaseSettings({"key": "val"})})