From 82e280dde19e70d0bce2809813deef2bad33e506 Mon Sep 17 00:00:00 2001 From: Adrian Chaves Date: Wed, 10 Jun 2026 12:05:07 +0200 Subject: [PATCH] Fix mypy and pylint issues introduced by the xtractmime merge - Add xtractmime to the mypy ignore_missing_imports overrides (no py.typed marker in that library) - Narrow Content-Disposition header via a local variable so mypy can see it is non-None inside the if-block - Remove a redundant local re-import of HtmlResponse/TextResponse inside open_in_browser (they are already imported at module level, fixing pylint W0404 reimported) - Add Any annotations to _unmark() in test_responsetypes.py so mypy does not raise no-untyped-call when it is called from a typed context Co-Authored-By: Claude Sonnet 4.6 --- pyproject.toml | 2 ++ scrapy/utils/response.py | 12 +++--------- tests/test_responsetypes.py | 6 +++++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 50322b17a..8cd7310ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,6 +133,8 @@ module = [ "pytest_twisted", "robotexclusionrulesparser", "testfixtures", + "xtractmime", + "xtractmime.*", "zope.interface.*", ] ignore_missing_imports = true diff --git a/scrapy/utils/response.py b/scrapy/utils/response.py index 470f6431f..a27ad62dc 100644 --- a/scrapy/utils/response.py +++ b/scrapy/utils/response.py @@ -94,13 +94,10 @@ def _get_encoding_or_mime_type_from_headers( ) ): return None, headers[b"Content-Type"] - if headers.get(b"Content-Disposition"): + content_disposition = headers.get(b"Content-Disposition") + if content_disposition: path = ( - headers[b"Content-Disposition"] - .split(b";")[-1] - .split(b"=")[-1] - .strip(b"\"'") - .decode() + content_disposition.split(b";")[-1].split(b"=")[-1].strip(b"\"'").decode() ) encoding, mime_type = _get_encoding_or_mime_type_from_path(path) if encoding: @@ -262,9 +259,6 @@ def open_in_browser( if "item name" not in response.body: open_in_browser(response) """ - # circular imports - from scrapy.http import HtmlResponse, TextResponse # noqa: PLC0415 - # XXX: this implementation is a bit dirty and could be improved body = response.body if isinstance(response, HtmlResponse): diff --git a/tests/test_responsetypes.py b/tests/test_responsetypes.py index 3b71567b8..73b9aaf2b 100644 --- a/tests/test_responsetypes.py +++ b/tests/test_responsetypes.py @@ -1,3 +1,7 @@ +from __future__ import annotations + +from typing import Any + import pytest from scrapy.http import ( @@ -13,7 +17,7 @@ from scrapy.responsetypes import responsetypes from .test_utils_response import POST_XTRACTMIME_SCENARIOS, PRE_XTRACTMIME_SCENARIOS -def _unmark(item): +def _unmark(item: Any) -> Any: return pytest.param(*item.values)