From 360b96305aa673b4dee277059717fb87ac55088c Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Mon, 18 Jul 2022 14:33:12 +0800 Subject: [PATCH] Upgrade pre-commit hooks Also fix some Mypy errors newly found with upgrade. --- .pre-commit-config.yaml | 6 +++--- .../_internal/metadata/importlib/_compat.py | 4 +++- src/pip/_internal/metadata/importlib/_dists.py | 18 ++++++++++++++++-- src/pip/_internal/network/lazy_wheel.py | 4 ++-- src/pip/_internal/network/session.py | 2 +- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4d88637ffac..6dde3d6ff2d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ exclude: 'src/pip/_vendor/' repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 + rev: v4.3.0 hooks: - id: check-builtin-literals - id: check-added-large-files @@ -17,7 +17,7 @@ repos: exclude: .patch - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 22.6.0 hooks: - id: black @@ -39,7 +39,7 @@ repos: files: \.py$ - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.942 + rev: v0.961 hooks: - id: mypy exclude: tests/data diff --git a/src/pip/_internal/metadata/importlib/_compat.py b/src/pip/_internal/metadata/importlib/_compat.py index 2bc6bfd27ef..e0879807ab9 100644 --- a/src/pip/_internal/metadata/importlib/_compat.py +++ b/src/pip/_internal/metadata/importlib/_compat.py @@ -13,7 +13,9 @@ class BasePath(Protocol): in both classes *that we need*. """ - name: str + @property + def name(self) -> str: + raise NotImplementedError() @property def parent(self) -> "BasePath": diff --git a/src/pip/_internal/metadata/importlib/_dists.py b/src/pip/_internal/metadata/importlib/_dists.py index ba039c4bfce..fbf9a93218a 100644 --- a/src/pip/_internal/metadata/importlib/_dists.py +++ b/src/pip/_internal/metadata/importlib/_dists.py @@ -3,7 +3,16 @@ import os import pathlib import zipfile -from typing import Collection, Dict, Iterable, Iterator, Mapping, Optional, Sequence +from typing import ( + Collection, + Dict, + Iterable, + Iterator, + Mapping, + Optional, + Sequence, + cast, +) from pip._vendor.packaging.requirements import Requirement from pip._vendor.packaging.utils import NormalizedName, canonicalize_name @@ -173,7 +182,12 @@ def iter_entry_points(self) -> Iterable[BaseEntryPoint]: return self._dist.entry_points def _metadata_impl(self) -> email.message.Message: - return self._dist.metadata + # From Python 3.10+, importlib.metadata declares PackageMetadata as the + # return type. This protocol is unfortunately a disaster now and misses + # a ton of fields that we need, including get() and get_payload(). We + # rely on the implementation that the object is actually a Message now, + # until upstream can improve the protocol. (python/cpython#94952) + return cast(email.message.Message, self._dist.metadata) def iter_provided_extras(self) -> Iterable[str]: return ( diff --git a/src/pip/_internal/network/lazy_wheel.py b/src/pip/_internal/network/lazy_wheel.py index b0de535669d..2d1ddaa8981 100644 --- a/src/pip/_internal/network/lazy_wheel.py +++ b/src/pip/_internal/network/lazy_wheel.py @@ -135,8 +135,8 @@ def __enter__(self) -> "LazyZipOverHTTP": self._file.__enter__() return self - def __exit__(self, *exc: Any) -> Optional[bool]: - return self._file.__exit__(*exc) + def __exit__(self, *exc: Any) -> None: + self._file.__exit__(*exc) @contextmanager def _stay(self) -> Generator[None, None, None]: diff --git a/src/pip/_internal/network/session.py b/src/pip/_internal/network/session.py index f5b834d090d..e512ac78464 100644 --- a/src/pip/_internal/network/session.py +++ b/src/pip/_internal/network/session.py @@ -465,7 +465,7 @@ def is_secure_origin(self, location: Link) -> bool: continue try: - addr = ipaddress.ip_address(origin_host) + addr = ipaddress.ip_address(origin_host or "") network = ipaddress.ip_network(secure_host) except ValueError: # We don't have both a valid address or a valid network, so