Skip to content

Commit 2b8b995

Browse files
authored
refactor: Fix minor typing of pip._internal.{utils, exceptions, locations, self_outdated_check} (#13622)
1 parent 750ccc0 commit 2b8b995

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

src/pip/_internal/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
if TYPE_CHECKING:
2828
from hashlib import _Hash
2929

30-
from pip._vendor.requests.models import Request, Response
30+
from pip._vendor.requests.models import PreparedRequest, Request, Response
3131

3232
from pip._internal.metadata import BaseDistribution
3333
from pip._internal.network.download import _FileDownload
@@ -314,7 +314,7 @@ def __init__(
314314
self,
315315
error_msg: str,
316316
response: Response | None = None,
317-
request: Request | None = None,
317+
request: Request | PreparedRequest | None = None,
318318
) -> None:
319319
"""
320320
Initialize NetworkConnectionError with `request` and `response`

src/pip/_internal/locations/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pathlib
77
import sys
88
import sysconfig
9-
from typing import Any
109

1110
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
1211
from pip._internal.utils.compat import WINDOWS
@@ -134,7 +133,7 @@ def _looks_like_red_hat_scheme() -> bool:
134133
from distutils.command.install import install
135134
from distutils.dist import Distribution
136135

137-
cmd: Any = install(Distribution())
136+
cmd = install(Distribution())
138137
cmd.finalize_options()
139138
return (
140139
cmd.exec_prefix == f"{os.path.normpath(sys.exec_prefix)}/local"

src/pip/_internal/locations/_sysconfig.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import sysconfig
7+
from typing import Callable
78

89
from pip._internal.exceptions import InvalidSchemeCombination, UserInstallationInvalid
910
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
@@ -24,7 +25,9 @@
2425

2526
_AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names())
2627

27-
_PREFERRED_SCHEME_API = getattr(sysconfig, "get_preferred_scheme", None)
28+
_PREFERRED_SCHEME_API: Callable[[str], str] | None = getattr(
29+
sysconfig, "get_preferred_scheme", None
30+
)
2831

2932

3033
def _should_use_osx_framework_prefix() -> bool:

src/pip/_internal/self_outdated_check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import os.path
1010
import sys
1111
from dataclasses import dataclass
12-
from typing import Any, Callable
12+
from typing import Callable
1313

1414
from pip._vendor.packaging.version import Version
1515
from pip._vendor.packaging.version import parse as parse_version
@@ -61,7 +61,7 @@ def _convert_date(isodate: str) -> datetime.datetime:
6161

6262
class SelfCheckState:
6363
def __init__(self, cache_dir: str) -> None:
64-
self._state: dict[str, Any] = {}
64+
self._state: dict[str, str] = {}
6565
self._statefile_path = None
6666

6767
# Try to load the existing state

src/pip/_internal/utils/misc.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,18 @@ def __repr__(self) -> str:
541541
def __str__(self) -> str:
542542
return self.redacted
543543

544-
# This is useful for testing.
545-
def __eq__(self, other: Any) -> bool:
546-
if type(self) is not type(other):
547-
return False
548-
549-
# The string being used for redaction doesn't also have to match,
550-
# just the raw, original string.
551-
return self.secret == other.secret
544+
def __eq__(self, other: object) -> bool:
545+
# Equality is particularly useful for testing.
546+
if type(self) is type(other):
547+
# The string being used for redaction doesn't also have to match,
548+
# just the raw, original string.
549+
return self.secret == cast(HiddenText, other).secret
550+
return NotImplemented
551+
552+
# Disable hashing, since we have a custom __eq__ and don't need hash-ability
553+
# (yet). The only required property of hashing is that objects which compare
554+
# equal have the same hash value.
555+
__hash__ = None # type: ignore[assignment]
552556

553557

554558
def hide_value(value: str) -> HiddenText:

0 commit comments

Comments
 (0)