From 9d4989a0d4cb0db633d7b8c32fb0a28c4e7130fa Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:31:50 -0500 Subject: [PATCH 01/16] Update mypy to 1.6.1 --- .pre-commit-config.yaml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2c576d90a5b..305839e5f99 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,20 +27,20 @@ repos: - id: ruff - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.961 + rev: v1.6.1 hooks: - id: mypy exclude: tests/data args: ["--pretty", "--show-error-codes"] additional_dependencies: [ - 'keyring==23.0.1', - 'nox==2021.6.12', + 'keyring==24.2.0', + 'nox==2023.4.22', 'pytest', - 'types-docutils==0.18.3', - 'types-setuptools==57.4.14', - 'types-freezegun==1.1.9', - 'types-six==1.16.15', - 'types-pyyaml==6.0.12.2', + 'types-docutils==0.20.0.3', + 'types-setuptools==68.2.0.0', + 'types-freezegun==1.1.10', + 'types-six==1.16.21.9', + 'types-pyyaml==6.0.12.12', ] - repo: https://github.com/pre-commit/pygrep-hooks From fd176223a593aee42adf66c3d928084a983434a5 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:33:29 -0500 Subject: [PATCH 02/16] Fix mypy "Source file found twice under different module names" error --- tools/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tools/__init__.py diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 00000000000..e69de29bb2d From efcdd2361ade7528ddd94b996990926824d7e875 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:34:52 -0500 Subject: [PATCH 03/16] Ignore type of intialized abstract class in tests --- tests/unit/metadata/test_metadata.py | 6 +++--- tests/unit/test_resolution_legacy_resolver.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/metadata/test_metadata.py b/tests/unit/metadata/test_metadata.py index 47093fb54d1..ccc8ceb2e75 100644 --- a/tests/unit/metadata/test_metadata.py +++ b/tests/unit/metadata/test_metadata.py @@ -23,7 +23,7 @@ def test_dist_get_direct_url_no_metadata(mock_read_text: mock.Mock) -> None: class FakeDistribution(BaseDistribution): pass - dist = FakeDistribution() + dist = FakeDistribution() # type: ignore assert dist.direct_url is None mock_read_text.assert_called_once_with(DIRECT_URL_METADATA_NAME) @@ -35,7 +35,7 @@ def test_dist_get_direct_url_invalid_json( class FakeDistribution(BaseDistribution): canonical_name = cast(NormalizedName, "whatever") # Needed for error logging. - dist = FakeDistribution() + dist = FakeDistribution() # type: ignore with caplog.at_level(logging.WARNING): assert dist.direct_url is None @@ -84,7 +84,7 @@ def test_dist_get_direct_url_valid_metadata(mock_read_text: mock.Mock) -> None: class FakeDistribution(BaseDistribution): pass - dist = FakeDistribution() + dist = FakeDistribution() # type: ignore direct_url = dist.direct_url assert direct_url is not None mock_read_text.assert_called_once_with(DIRECT_URL_METADATA_NAME) diff --git a/tests/unit/test_resolution_legacy_resolver.py b/tests/unit/test_resolution_legacy_resolver.py index 8b9d1a58a33..a1c45b01b96 100644 --- a/tests/unit/test_resolution_legacy_resolver.py +++ b/tests/unit/test_resolution_legacy_resolver.py @@ -252,7 +252,7 @@ class NotWorkingFakeDist(FakeDist): def metadata(self) -> email.message.Message: raise FileNotFoundError(metadata_name) - dist = make_fake_dist(klass=NotWorkingFakeDist) + dist = make_fake_dist(klass=NotWorkingFakeDist) # type: ignore with pytest.raises(NoneMetadataError) as exc: _check_dist_requires_python( From 90dda53d02946bfc84b384c616e769e27eab08e1 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:35:50 -0500 Subject: [PATCH 04/16] Use more specific type ignore method-assign --- tests/lib/configuration_helpers.py | 2 +- tests/unit/test_base_command.py | 6 +++--- tests/unit/test_configuration.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/lib/configuration_helpers.py b/tests/lib/configuration_helpers.py index ec824ffd3b8..b6e398c5bf1 100644 --- a/tests/lib/configuration_helpers.py +++ b/tests/lib/configuration_helpers.py @@ -38,7 +38,7 @@ def overridden() -> None: old() # https://github.com/python/mypy/issues/2427 - self.configuration._load_config_files = overridden # type: ignore[assignment] + self.configuration._load_config_files = overridden # type: ignore[method-assign] @contextlib.contextmanager def tmpfile(self, contents: str) -> Iterator[str]: diff --git a/tests/unit/test_base_command.py b/tests/unit/test_base_command.py index daec5fc6c65..44dae384a75 100644 --- a/tests/unit/test_base_command.py +++ b/tests/unit/test_base_command.py @@ -151,7 +151,7 @@ def assert_helpers_set(options: Values, args: List[str]) -> int: c = Command("fake", "fake") # https://github.com/python/mypy/issues/2427 - c.run = Mock(side_effect=assert_helpers_set) # type: ignore[assignment] + c.run = Mock(side_effect=assert_helpers_set) # type: ignore[method-assign] assert c.main(["fake"]) == SUCCESS c.run.assert_called_once() @@ -176,7 +176,7 @@ def create_temp_dirs(options: Values, args: List[str]) -> int: c = Command("fake", "fake") # https://github.com/python/mypy/issues/2427 - c.run = Mock(side_effect=create_temp_dirs) # type: ignore[assignment] + c.run = Mock(side_effect=create_temp_dirs) # type: ignore[method-assign] assert c.main(["fake"]) == SUCCESS c.run.assert_called_once() assert os.path.exists(Holder.value) == exists @@ -200,6 +200,6 @@ def create_temp_dirs(options: Values, args: List[str]) -> int: c = Command("fake", "fake") # https://github.com/python/mypy/issues/2427 - c.run = Mock(side_effect=create_temp_dirs) # type: ignore[assignment] + c.run = Mock(side_effect=create_temp_dirs) # type: ignore[method-assign] assert c.main(["fake"]) == SUCCESS c.run.assert_called_once() diff --git a/tests/unit/test_configuration.py b/tests/unit/test_configuration.py index c6b44d45aad..1a0acb7b411 100644 --- a/tests/unit/test_configuration.py +++ b/tests/unit/test_configuration.py @@ -215,7 +215,7 @@ def test_site_modification(self) -> None: # Mock out the method mymock = MagicMock(spec=self.configuration._mark_as_modified) # https://github.com/python/mypy/issues/2427 - self.configuration._mark_as_modified = mymock # type: ignore[assignment] + self.configuration._mark_as_modified = mymock # type: ignore[method-assign] self.configuration.set_value("test.hello", "10") @@ -231,7 +231,7 @@ def test_user_modification(self) -> None: # Mock out the method mymock = MagicMock(spec=self.configuration._mark_as_modified) # https://github.com/python/mypy/issues/2427 - self.configuration._mark_as_modified = mymock # type: ignore[assignment] + self.configuration._mark_as_modified = mymock # type: ignore[method-assign] self.configuration.set_value("test.hello", "10") @@ -250,7 +250,7 @@ def test_global_modification(self) -> None: # Mock out the method mymock = MagicMock(spec=self.configuration._mark_as_modified) # https://github.com/python/mypy/issues/2427 - self.configuration._mark_as_modified = mymock # type: ignore[assignment] + self.configuration._mark_as_modified = mymock # type: ignore[method-assign] self.configuration.set_value("test.hello", "10") From b23f226df38d8d4953fffe419995e0fdb37e9c0d Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:36:29 -0500 Subject: [PATCH 05/16] Type ignore for message.get_all --- src/pip/_internal/metadata/_json.py | 4 ++-- tests/lib/test_wheel.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pip/_internal/metadata/_json.py b/src/pip/_internal/metadata/_json.py index 336b52f1efd..27362fc726c 100644 --- a/src/pip/_internal/metadata/_json.py +++ b/src/pip/_internal/metadata/_json.py @@ -64,10 +64,10 @@ def sanitise_header(h: Union[Header, str]) -> str: key = json_name(field) if multi: value: Union[str, List[str]] = [ - sanitise_header(v) for v in msg.get_all(field) + sanitise_header(v) for v in msg.get_all(field) # type: ignore ] else: - value = sanitise_header(msg.get(field)) + value = sanitise_header(msg.get(field)) # type: ignore if key == "keywords": # Accept both comma-separated and space-separated # forms, for better compatibility with old data. diff --git a/tests/lib/test_wheel.py b/tests/lib/test_wheel.py index 86994c28e57..ffe96cc4335 100644 --- a/tests/lib/test_wheel.py +++ b/tests/lib/test_wheel.py @@ -19,12 +19,12 @@ def test_message_from_dict_one_value() -> None: message = message_from_dict({"a": "1"}) - assert set(message.get_all("a")) == {"1"} + assert set(message.get_all("a")) == {"1"} # type: ignore def test_message_from_dict_multiple_values() -> None: message = message_from_dict({"a": ["1", "2"]}) - assert set(message.get_all("a")) == {"1", "2"} + assert set(message.get_all("a")) == {"1", "2"} # type: ignore def message_from_bytes(contents: bytes) -> Message: @@ -67,7 +67,7 @@ def test_make_metadata_file_custom_value_list() -> None: f = default_make_metadata(updates={"a": ["1", "2"]}) assert f is not None message = default_metadata_checks(f) - assert set(message.get_all("a")) == {"1", "2"} + assert set(message.get_all("a")) == {"1", "2"} # type: ignore def test_make_metadata_file_custom_value_overrides() -> None: @@ -101,7 +101,7 @@ def default_wheel_metadata_checks(f: File) -> Message: assert message.get_all("Wheel-Version") == ["1.0"] assert message.get_all("Generator") == ["pip-test-suite"] assert message.get_all("Root-Is-Purelib") == ["true"] - assert set(message.get_all("Tag")) == {"py2-none-any", "py3-none-any"} + assert set(message.get_all("Tag")) == {"py2-none-any", "py3-none-any"} # type: ignore return message @@ -122,7 +122,7 @@ def test_make_wheel_metadata_file_custom_value_list() -> None: f = default_make_wheel_metadata(updates={"a": ["1", "2"]}) assert f is not None message = default_wheel_metadata_checks(f) - assert set(message.get_all("a")) == {"1", "2"} + assert set(message.get_all("a")) == {"1", "2"} # type: ignore def test_make_wheel_metadata_file_custom_value_override() -> None: From ce174324a98549adab7b0860c23163d05de76e70 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:36:51 -0500 Subject: [PATCH 06/16] Remove unused type ignore --- src/pip/_internal/locations/_distutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/locations/_distutils.py b/src/pip/_internal/locations/_distutils.py index 48689f5fbe4..eae5f4c9451 100644 --- a/src/pip/_internal/locations/_distutils.py +++ b/src/pip/_internal/locations/_distutils.py @@ -57,7 +57,7 @@ def distutils_scheme( d.parse_config_files() except UnicodeDecodeError: # Typeshed does not include find_config_files() for some reason. - paths = d.find_config_files() # type: ignore + paths = d.find_config_files() logger.warning( "Ignore distutils configs in %s due to encoding errors.", ", ".join(os.path.basename(p) for p in paths), From 480af38de5a314b47ae8c938bf48680ff75b67b9 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:37:51 -0500 Subject: [PATCH 07/16] Add SizedBuffer type for xmlrpc.client.Transport subclass --- src/pip/_internal/network/xmlrpc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/network/xmlrpc.py b/src/pip/_internal/network/xmlrpc.py index 4a7d55d0e50..45ed0eb6b3c 100644 --- a/src/pip/_internal/network/xmlrpc.py +++ b/src/pip/_internal/network/xmlrpc.py @@ -13,6 +13,8 @@ if TYPE_CHECKING: from xmlrpc.client import _HostType, _Marshallable + from _typeshed import SizedBuffer + logger = logging.getLogger(__name__) @@ -33,7 +35,7 @@ def request( self, host: "_HostType", handler: str, - request_body: bytes, + request_body: SizedBuffer, verbose: bool = False, ) -> Tuple["_Marshallable", ...]: assert isinstance(host, str) From c67c0b79716afe33281366ba7e8a1b026840a290 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:38:33 -0500 Subject: [PATCH 08/16] Add Self type for RequestHandlerClass in test --- tests/conftest.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6ae2e6d62d8..a752cdb0864 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,6 +14,7 @@ from pathlib import Path from textwrap import dedent from typing import ( + TYPE_CHECKING, Any, AnyStr, Callable, @@ -58,6 +59,9 @@ from tests.lib.server import MockServer, make_mock_server from tests.lib.venv import VirtualEnvironment, VirtualEnvironmentType +if TYPE_CHECKING: + from typing import Self + def pytest_addoption(parser: Parser) -> None: parser.addoption( @@ -941,7 +945,7 @@ def html_index_with_onetime_server( """ class InDirectoryServer(http.server.ThreadingHTTPServer): - def finish_request(self, request: Any, client_address: Any) -> None: + def finish_request(self: Self, request: Any, client_address: Any) -> None: self.RequestHandlerClass( request, client_address, From 10b129054f7cda6630ae56c8a64b34aeb0411330 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:39:07 -0500 Subject: [PATCH 09/16] Add type ignore for shutil.rmtree onexc handler --- src/pip/_internal/utils/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index 78060e86417..a2b6f7cb1de 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -145,7 +145,7 @@ def rmtree( ) if sys.version_info >= (3, 12): # See https://docs.python.org/3.12/whatsnew/3.12.html#shutil. - shutil.rmtree(dir, onexc=handler) + shutil.rmtree(dir, onexc=handler) # type: ignore else: shutil.rmtree(dir, onerror=handler) From e67f8cf0da8043f86567c0b040b431f4a490030b Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:44:38 -0500 Subject: [PATCH 10/16] Quote SizedBuffer --- src/pip/_internal/network/xmlrpc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/network/xmlrpc.py b/src/pip/_internal/network/xmlrpc.py index 45ed0eb6b3c..22ec8d2f4a6 100644 --- a/src/pip/_internal/network/xmlrpc.py +++ b/src/pip/_internal/network/xmlrpc.py @@ -35,7 +35,7 @@ def request( self, host: "_HostType", handler: str, - request_body: SizedBuffer, + request_body: "SizedBuffer", verbose: bool = False, ) -> Tuple["_Marshallable", ...]: assert isinstance(host, str) From ac2c42c110748c5e1df8f838959fd4fdd47fae0c Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:47:02 -0500 Subject: [PATCH 11/16] Add news entry --- news/12389.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/12389.bugfix.rst diff --git a/news/12389.bugfix.rst b/news/12389.bugfix.rst new file mode 100644 index 00000000000..1cd373e22e5 --- /dev/null +++ b/news/12389.bugfix.rst @@ -0,0 +1 @@ +Update to using mypy 1.6.1 and fix appropriatew types From 122418051af2093bacf949eaa5eaa3c5b6bf5cd0 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:48:06 -0500 Subject: [PATCH 12/16] Remove no longer correct comment --- src/pip/_internal/locations/_distutils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pip/_internal/locations/_distutils.py b/src/pip/_internal/locations/_distutils.py index eae5f4c9451..0e18c6e1e14 100644 --- a/src/pip/_internal/locations/_distutils.py +++ b/src/pip/_internal/locations/_distutils.py @@ -56,7 +56,6 @@ def distutils_scheme( try: d.parse_config_files() except UnicodeDecodeError: - # Typeshed does not include find_config_files() for some reason. paths = d.find_config_files() logger.warning( "Ignore distutils configs in %s due to encoding errors.", From c6071c3375c84edb5a23d2dabcfd52c7a843d3b7 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:50:36 -0500 Subject: [PATCH 13/16] Update self import --- tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a752cdb0864..a758a62dba5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -60,7 +60,7 @@ from tests.lib.venv import VirtualEnvironment, VirtualEnvironmentType if TYPE_CHECKING: - from typing import Self + from pip._vendor.typing_extensions import Self def pytest_addoption(parser: Parser) -> None: @@ -945,7 +945,7 @@ def html_index_with_onetime_server( """ class InDirectoryServer(http.server.ThreadingHTTPServer): - def finish_request(self: Self, request: Any, client_address: Any) -> None: + def finish_request(self: "Self", request: Any, client_address: Any) -> None: self.RequestHandlerClass( request, client_address, From bcbf1771474b10d182c2bd5c097f1177b327cd2d Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Sun, 5 Nov 2023 14:52:34 -0500 Subject: [PATCH 14/16] Also ignore type onerror=handler --- src/pip/_internal/utils/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index a2b6f7cb1de..3cff02de356 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -147,7 +147,7 @@ def rmtree( # See https://docs.python.org/3.12/whatsnew/3.12.html#shutil. shutil.rmtree(dir, onexc=handler) # type: ignore else: - shutil.rmtree(dir, onerror=handler) + shutil.rmtree(dir, onerror=handler) # type: ignore def _onerror_ignore(*_args: Any) -> None: From 280d91432d888d2de4c74146fe7c9fc3c43c7d3b Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Mon, 6 Nov 2023 20:23:21 -0500 Subject: [PATCH 15/16] Update news entry --- news/12389.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/12389.bugfix.rst b/news/12389.bugfix.rst index 1cd373e22e5..cd30d061fa5 100644 --- a/news/12389.bugfix.rst +++ b/news/12389.bugfix.rst @@ -1 +1 @@ -Update to using mypy 1.6.1 and fix appropriatew types +Update to using mypy 1.6.1 and fix/ignore types From 84a3ab4a7f2243d027841b3b849b0bdee4d46505 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Mon, 6 Nov 2023 20:23:55 -0500 Subject: [PATCH 16/16] Update news entry --- news/12389.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/12389.bugfix.rst b/news/12389.bugfix.rst index cd30d061fa5..84871873328 100644 --- a/news/12389.bugfix.rst +++ b/news/12389.bugfix.rst @@ -1 +1 @@ -Update to using mypy 1.6.1 and fix/ignore types +Update mypy to 1.6.1 and fix/ignore types