Skip to content

Commit 19d88fb

Browse files
committed
Upgrade mypy
Mostly straightforward. The new message.get_all type ignores out of fear it may return None are minorly annoying. You can see some of the extra errors on the pip codebase in the mypy_primer of the typeshed PR that made the change python/typeshed#9620 The abstract errors are because FakeDistribution doesn't actually implement any of the Protocol it claims to implement
1 parent eddd9dd commit 19d88fb

File tree

13 files changed

+34
-30
lines changed

13 files changed

+34
-30
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repos:
2828
- id: ruff
2929

3030
- repo: https://github.com/pre-commit/mirrors-mypy
31-
rev: v0.961
31+
rev: v1.5.1
3232
hooks:
3333
- id: mypy
3434
exclude: tests/data

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ allow_subclassing_any = True
4444
allow_untyped_calls = True
4545
warn_return_any = False
4646
ignore_missing_imports = True
47+
explicit_package_bases = True
4748

4849
[mypy-pip._internal.utils._jaraco_text]
4950
ignore_errors = True

src/pip/_internal/locations/_distutils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def distutils_scheme(
5656
try:
5757
d.parse_config_files()
5858
except UnicodeDecodeError:
59-
# Typeshed does not include find_config_files() for some reason.
60-
paths = d.find_config_files() # type: ignore
59+
paths = d.find_config_files()
6160
logger.warning(
6261
"Ignore distutils configs in %s due to encoding errors.",
6362
", ".join(os.path.basename(p) for p in paths),

src/pip/_internal/metadata/_json.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ def sanitise_header(h: Union[Header, str]) -> str:
6464
key = json_name(field)
6565
if multi:
6666
value: Union[str, List[str]] = [
67-
sanitise_header(v) for v in msg.get_all(field)
67+
sanitise_header(v)
68+
for v in msg.get_all(field) # type: ignore[union-attr]
6869
]
6970
else:
70-
value = sanitise_header(msg.get(field))
71+
value = sanitise_header(msg.get(field)) # type: ignore[arg-type]
7172
if key == "keywords":
7273
# Accept both comma-separated and space-separated
7374
# forms, for better compatibility with old data.

src/pip/_internal/network/xmlrpc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def request(
3333
self,
3434
host: "_HostType",
3535
handler: str,
36-
request_body: bytes,
36+
# Should ideally be typed with SizedBuffer,
37+
# but that's not easily usable till Python 3.11
38+
request_body: bytes, # type: ignore[override]
3739
verbose: bool = False,
3840
) -> Tuple["_Marshallable", ...]:
3941
assert isinstance(host, str)

src/pip/_internal/utils/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
VersionInfo = Tuple[int, int, int]
7070
NetlocTuple = Tuple[str, Tuple[Optional[str], Optional[str]]]
7171
OnExc = Callable[[FunctionType, Path, BaseException], Any]
72-
OnErr = Callable[[FunctionType, Path, ExcInfo], Any]
72+
OnErr = Callable[[Callable[..., Any], str, ExcInfo], Any]
7373

7474

7575
def get_pip_version() -> str:

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ def html_index_with_onetime_server(
10311031
class InDirectoryServer(http.server.ThreadingHTTPServer):
10321032
def finish_request(self, request: Any, client_address: Any) -> None:
10331033
self.RequestHandlerClass(
1034-
request, client_address, self, directory=str(html_index_for_packages) # type: ignore[call-arg] # noqa: E501
1034+
request, client_address, self, directory=str(html_index_for_packages) # type: ignore[call-arg, arg-type] # noqa: E501
10351035
)
10361036

10371037
class Handler(OneTimeDownloadHandler):

tests/lib/configuration_helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def overridden() -> None:
3737
)
3838
old()
3939

40-
# https://github.com/python/mypy/issues/2427
41-
self.configuration._load_config_files = overridden # type: ignore[assignment]
40+
self.configuration._load_config_files = ( # type: ignore[method-assign]
41+
overridden
42+
)
4243

4344
@contextlib.contextmanager
4445
def tmpfile(self, contents: str) -> Iterator[str]:

tests/lib/test_wheel.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
def test_message_from_dict_one_value() -> None:
2121
message = message_from_dict({"a": "1"})
22-
assert set(message.get_all("a")) == {"1"}
22+
assert set(message.get_all("a")) == {"1"} # type: ignore[arg-type]
2323

2424

2525
def test_message_from_dict_multiple_values() -> None:
2626
message = message_from_dict({"a": ["1", "2"]})
27-
assert set(message.get_all("a")) == {"1", "2"}
27+
assert set(message.get_all("a")) == {"1", "2"} # type: ignore[arg-type]
2828

2929

3030
def message_from_bytes(contents: bytes) -> Message:
@@ -67,7 +67,7 @@ def test_make_metadata_file_custom_value_list() -> None:
6767
f = default_make_metadata(updates={"a": ["1", "2"]})
6868
assert f is not None
6969
message = default_metadata_checks(f)
70-
assert set(message.get_all("a")) == {"1", "2"}
70+
assert set(message.get_all("a")) == {"1", "2"} # type: ignore[arg-type]
7171

7272

7373
def test_make_metadata_file_custom_value_overrides() -> None:
@@ -101,7 +101,10 @@ def default_wheel_metadata_checks(f: File) -> Message:
101101
assert message.get_all("Wheel-Version") == ["1.0"]
102102
assert message.get_all("Generator") == ["pip-test-suite"]
103103
assert message.get_all("Root-Is-Purelib") == ["true"]
104-
assert set(message.get_all("Tag")) == {"py2-none-any", "py3-none-any"}
104+
assert set(message.get_all("Tag")) == { # type: ignore[arg-type]
105+
"py2-none-any",
106+
"py3-none-any",
107+
}
105108
return message
106109

107110

@@ -122,7 +125,7 @@ def test_make_wheel_metadata_file_custom_value_list() -> None:
122125
f = default_make_wheel_metadata(updates={"a": ["1", "2"]})
123126
assert f is not None
124127
message = default_wheel_metadata_checks(f)
125-
assert set(message.get_all("a")) == {"1", "2"}
128+
assert set(message.get_all("a")) == {"1", "2"} # type: ignore[arg-type]
126129

127130

128131
def test_make_wheel_metadata_file_custom_value_override() -> None:

tests/unit/metadata/test_metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_dist_get_direct_url_no_metadata(mock_read_text: mock.Mock) -> None:
2323
class FakeDistribution(BaseDistribution):
2424
pass
2525

26-
dist = FakeDistribution()
26+
dist = FakeDistribution() # type: ignore[abstract]
2727
assert dist.direct_url is None
2828
mock_read_text.assert_called_once_with(DIRECT_URL_METADATA_NAME)
2929

@@ -35,7 +35,7 @@ def test_dist_get_direct_url_invalid_json(
3535
class FakeDistribution(BaseDistribution):
3636
canonical_name = cast(NormalizedName, "whatever") # Needed for error logging.
3737

38-
dist = FakeDistribution()
38+
dist = FakeDistribution() # type: ignore[abstract]
3939
with caplog.at_level(logging.WARNING):
4040
assert dist.direct_url is None
4141

@@ -84,7 +84,7 @@ def test_dist_get_direct_url_valid_metadata(mock_read_text: mock.Mock) -> None:
8484
class FakeDistribution(BaseDistribution):
8585
pass
8686

87-
dist = FakeDistribution()
87+
dist = FakeDistribution() # type: ignore[abstract]
8888
direct_url = dist.direct_url
8989
assert direct_url is not None
9090
mock_read_text.assert_called_once_with(DIRECT_URL_METADATA_NAME)

tests/unit/test_base_command.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# mypy: disable-error-code="method-assign"
12
import logging
23
import os
34
import time
@@ -150,8 +151,7 @@ def assert_helpers_set(options: Values, args: List[str]) -> int:
150151
return SUCCESS
151152

152153
c = Command("fake", "fake")
153-
# https://github.com/python/mypy/issues/2427
154-
c.run = Mock(side_effect=assert_helpers_set) # type: ignore[assignment]
154+
c.run = Mock(side_effect=assert_helpers_set)
155155
assert c.main(["fake"]) == SUCCESS
156156
c.run.assert_called_once()
157157

@@ -175,8 +175,7 @@ def create_temp_dirs(options: Values, args: List[str]) -> int:
175175
return SUCCESS
176176

177177
c = Command("fake", "fake")
178-
# https://github.com/python/mypy/issues/2427
179-
c.run = Mock(side_effect=create_temp_dirs) # type: ignore[assignment]
178+
c.run = Mock(side_effect=create_temp_dirs)
180179
assert c.main(["fake"]) == SUCCESS
181180
c.run.assert_called_once()
182181
assert os.path.exists(Holder.value) == exists
@@ -200,6 +199,6 @@ def create_temp_dirs(options: Values, args: List[str]) -> int:
200199

201200
c = Command("fake", "fake")
202201
# https://github.com/python/mypy/issues/2427
203-
c.run = Mock(side_effect=create_temp_dirs) # type: ignore[assignment]
202+
c.run = Mock(side_effect=create_temp_dirs)
204203
assert c.main(["fake"]) == SUCCESS
205204
c.run.assert_called_once()

tests/unit/test_configuration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for all things related to the configuration
22
"""
3-
3+
# mypy: disable-error-code="method-assign"
44
import re
55
from unittest.mock import MagicMock
66

@@ -214,8 +214,7 @@ def test_site_modification(self) -> None:
214214

215215
# Mock out the method
216216
mymock = MagicMock(spec=self.configuration._mark_as_modified)
217-
# https://github.com/python/mypy/issues/2427
218-
self.configuration._mark_as_modified = mymock # type: ignore[assignment]
217+
self.configuration._mark_as_modified = mymock
219218

220219
self.configuration.set_value("test.hello", "10")
221220

@@ -231,7 +230,7 @@ def test_user_modification(self) -> None:
231230
# Mock out the method
232231
mymock = MagicMock(spec=self.configuration._mark_as_modified)
233232
# https://github.com/python/mypy/issues/2427
234-
self.configuration._mark_as_modified = mymock # type: ignore[assignment]
233+
self.configuration._mark_as_modified = mymock
235234

236235
self.configuration.set_value("test.hello", "10")
237236

@@ -249,8 +248,7 @@ def test_global_modification(self) -> None:
249248

250249
# Mock out the method
251250
mymock = MagicMock(spec=self.configuration._mark_as_modified)
252-
# https://github.com/python/mypy/issues/2427
253-
self.configuration._mark_as_modified = mymock # type: ignore[assignment]
251+
self.configuration._mark_as_modified = mymock
254252

255253
self.configuration.set_value("test.hello", "10")
256254

tests/unit/test_resolution_legacy_resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class NotWorkingFakeDist(FakeDist):
252252
def metadata(self) -> email.message.Message:
253253
raise FileNotFoundError(metadata_name)
254254

255-
dist = make_fake_dist(klass=NotWorkingFakeDist)
255+
dist = make_fake_dist(klass=NotWorkingFakeDist) # type: ignore[type-abstract]
256256

257257
with pytest.raises(NoneMetadataError) as exc:
258258
_check_dist_requires_python(

0 commit comments

Comments
 (0)