[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,7 +1,15 @@
import hashlib
from typing import TYPE_CHECKING, BinaryIO, Dict, Iterator, List
from __future__ import annotations
from pip._internal.exceptions import HashMismatch, HashMissing, InstallationError
import hashlib
from typing import BinaryIO
from typing import Dict
from typing import Iterator
from typing import List
from typing import TYPE_CHECKING
from pip._internal.exceptions import HashMismatch
from pip._internal.exceptions import HashMissing
from pip._internal.exceptions import InstallationError
from pip._internal.utils.misc import read_chunks
if TYPE_CHECKING:
@ -14,12 +22,12 @@ if TYPE_CHECKING:
# The recommended hash algo of the moment. Change this whenever the state of
# the art changes; it won't hurt backward compatibility.
FAVORITE_HASH = "sha256"
FAVORITE_HASH = 'sha256'
# Names of hashlib algorithms allowed by the --hash option and ``pip hash``
# Currently, those are the ones at least as collision-resistant as sha256.
STRONG_HASHES = ["sha256", "sha384", "sha512"]
STRONG_HASHES = ['sha256', 'sha384', 'sha512']
class Hashes:
@ -28,7 +36,7 @@ class Hashes:
"""
def __init__(self, hashes: Dict[str, List[str]] = None) -> None:
def __init__(self, hashes: dict[str, list[str]] = None) -> None:
"""
:param hashes: A dict of algorithm names pointing to lists of allowed
hex digests
@ -40,7 +48,7 @@ class Hashes:
allowed[alg] = sorted(keys)
self._allowed = allowed
def __and__(self, other: "Hashes") -> "Hashes":
def __and__(self, other: Hashes) -> Hashes:
if not isinstance(other, Hashes):
return NotImplemented
@ -79,7 +87,7 @@ class Hashes:
try:
gots[hash_name] = hashlib.new(hash_name)
except (ValueError, TypeError):
raise InstallationError(f"Unknown hash name: {hash_name}")
raise InstallationError(f'Unknown hash name: {hash_name}')
for chunk in chunks:
for hash in gots.values():
@ -90,7 +98,7 @@ class Hashes:
return
self._raise(gots)
def _raise(self, gots: Dict[str, "_Hash"]) -> "NoReturn":
def _raise(self, gots: dict[str, _Hash]) -> NoReturn:
raise HashMismatch(self._allowed, gots)
def check_against_file(self, file: BinaryIO) -> None:
@ -102,7 +110,7 @@ class Hashes:
return self.check_against_chunks(read_chunks(file))
def check_against_path(self, path: str) -> None:
with open(path, "rb") as file:
with open(path, 'rb') as file:
return self.check_against_file(file)
def __bool__(self) -> bool:
@ -116,13 +124,13 @@ class Hashes:
def __hash__(self) -> int:
return hash(
",".join(
','.join(
sorted(
":".join((alg, digest))
':'.join((alg, digest))
for alg, digest_list in self._allowed.items()
for digest in digest_list
)
)
),
),
)
@ -140,5 +148,5 @@ class MissingHashes(Hashes):
# empty list, it will never match, so an error will always raise.
super().__init__(hashes={FAVORITE_HASH: []})
def _raise(self, gots: Dict[str, "_Hash"]) -> "NoReturn":
def _raise(self, gots: dict[str, _Hash]) -> NoReturn:
raise HashMissing(gots[FAVORITE_HASH].hexdigest())