1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Typing fixes

This commit is contained in:
Tzu-ping Chung 2021-03-10 13:06:35 +08:00
parent 4807de8e73
commit 56a8f3d8bd
16 changed files with 106 additions and 88 deletions

View file

@ -4,7 +4,7 @@ import os
import sys
from optparse import Values
from types import ModuleType
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional
import pip._vendor
from pip._vendor.certifi import where
@ -24,7 +24,7 @@ logger = logging.getLogger(__name__)
def show_value(name, value):
# type: (str, Optional[str]) -> None
# type: (str, Any) -> None
logger.info('%s: %s', name, value)

View file

@ -5,7 +5,7 @@ import os
import shutil
import site
from optparse import SUPPRESS_HELP, Values
from typing import Iterable, List, Optional
from typing import TYPE_CHECKING, Iterable, List, Optional
from pip._vendor.packaging.utils import canonicalize_name
@ -22,7 +22,7 @@ from pip._internal.exceptions import CommandError, InstallationError
from pip._internal.locations import get_scheme
from pip._internal.metadata import get_environment
from pip._internal.models.format_control import FormatControl
from pip._internal.operations.check import ConflictDetails, check_install_conflicts
from pip._internal.operations.check import check_install_conflicts
from pip._internal.req import install_given_reqs
from pip._internal.req.req_install import InstallRequirement
from pip._internal.req.req_tracker import get_requirement_tracker
@ -42,6 +42,9 @@ from pip._internal.wheel_builder import (
should_build_for_install_command,
)
if TYPE_CHECKING:
from pip._internal.operations.check import ConflictDetails
logger = logging.getLogger(__name__)
@ -49,7 +52,7 @@ def get_check_binary_allowed(format_control):
# type: (FormatControl) -> BinaryAllowedPredicate
def check_binary_allowed(req):
# type: (InstallRequirement) -> bool
canonical_name = canonicalize_name(req.name)
canonical_name = canonicalize_name(req.name or "")
allowed_formats = format_control.get_allowed_formats(canonical_name)
return "binary" in allowed_formats

View file

@ -1,11 +1,13 @@
import logging
import re
from typing import Container, Iterator, List, Optional
from typing import Container, Iterator, List, Optional, Union
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.version import LegacyVersion, Version
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
DistributionVersion = Union[LegacyVersion, Version]
logger = logging.getLogger(__name__)
@ -34,7 +36,7 @@ class BaseDistribution:
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> DistributionVersion
raise NotImplementedError()
@property

View file

@ -3,14 +3,13 @@ from typing import Iterator, List, Optional
from pip._vendor import pkg_resources
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.version import parse as parse_version
from pip._internal.utils import misc # TODO: Move definition here.
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
from .base import BaseDistribution, BaseEnvironment
from .base import BaseDistribution, BaseEnvironment, DistributionVersion
class Distribution(BaseDistribution):
@ -45,7 +44,7 @@ class Distribution(BaseDistribution):
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> DistributionVersion
return parse_version(self._dist.version)
@property

View file

@ -1,4 +1,3 @@
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.version import parse as parse_version
from pip._internal.models.link import Link
@ -14,7 +13,7 @@ class InstallationCandidate(KeyBasedCompareMixin):
def __init__(self, name, version, link):
# type: (str, str, Link) -> None
self.name = name
self.version = parse_version(version) # type: _BaseVersion
self.version = parse_version(version)
self.link = link
super().__init__(

View file

@ -295,7 +295,7 @@ class PipSession(requests.Session):
# Add a small amount of back off between failed requests in
# order to prevent hammering the service.
backoff_factor=0.25,
)
) # type: ignore
# Our Insecure HTTPAdapter disables HTTPS validation. It does not
# support caching so we'll use it for all http:// URLs.

View file

@ -3,7 +3,7 @@
import logging
from collections import namedtuple
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.pkg_resources import RequirementParseError
@ -12,23 +12,26 @@ from pip._internal.distributions import make_distribution_for_install_requiremen
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.misc import get_installed_distributions
if TYPE_CHECKING:
from pip._vendor.packaging.utils import NormalizedName
# Shorthands
PackageSet = Dict[NormalizedName, 'PackageDetails']
Missing = Tuple[str, Any]
Conflicting = Tuple[str, str, Any]
MissingDict = Dict[NormalizedName, List[Missing]]
ConflictingDict = Dict[NormalizedName, List[Conflicting]]
CheckResult = Tuple[MissingDict, ConflictingDict]
ConflictDetails = Tuple[PackageSet, CheckResult]
logger = logging.getLogger(__name__)
# Shorthands
PackageSet = Dict[str, 'PackageDetails']
Missing = Tuple[str, Any]
Conflicting = Tuple[str, str, Any]
MissingDict = Dict[str, List[Missing]]
ConflictingDict = Dict[str, List[Conflicting]]
CheckResult = Tuple[MissingDict, ConflictingDict]
ConflictDetails = Tuple[PackageSet, CheckResult]
PackageDetails = namedtuple('PackageDetails', ['version', 'requires'])
def create_package_set_from_installed(**kwargs):
# type: (**Any) -> Tuple[PackageSet, bool]
def create_package_set_from_installed(**kwargs: Any) -> Tuple["PackageSet", bool]:
"""Converts a list of distributions into a PackageSet.
"""
# Default to using all packages installed on the system
@ -59,7 +62,7 @@ def check_package_set(package_set, should_ignore=None):
missing = {}
conflicting = {}
for package_name in package_set:
for package_name, package_detail in package_set.items():
# Info about dependencies of package_name
missing_deps = set() # type: Set[Missing]
conflicting_deps = set() # type: Set[Conflicting]
@ -67,8 +70,8 @@ def check_package_set(package_set, should_ignore=None):
if should_ignore and should_ignore(package_name):
continue
for req in package_set[package_name].requires:
name = canonicalize_name(req.project_name) # type: str
for req in package_detail.requires:
name = canonicalize_name(req.project_name)
# Check if it's missing
if name not in package_set:
@ -114,7 +117,7 @@ def check_install_conflicts(to_install):
def _simulate_installation_of(to_install, package_set):
# type: (List[InstallRequirement], PackageSet) -> Set[str]
# type: (List[InstallRequirement], PackageSet) -> Set[NormalizedName]
"""Computes the version of packages after installing to_install.
"""
@ -136,7 +139,7 @@ def _simulate_installation_of(to_install, package_set):
def _create_whitelist(would_be_installed, package_set):
# type: (Set[str], PackageSet) -> Set[str]
# type: (Set[NormalizedName], PackageSet) -> Set[NormalizedName]
packages_affected = set(would_be_installed)
for package_name in package_set:

View file

@ -182,7 +182,7 @@ def parse_req_from_editable(editable_req):
if name is not None:
try:
req = Requirement(name)
req = Requirement(name) # type: Optional[Requirement]
except InvalidRequirement:
raise InstallationError(f"Invalid requirement: '{name}'")
else:
@ -335,7 +335,7 @@ def parse_req_from_line(name, line_source):
return text
return f'{text} (from {line_source})'
if req_as_string is not None:
def _parse_req_string(req_as_string: str) -> Requirement:
try:
req = Requirement(req_as_string)
except InvalidRequirement:
@ -363,6 +363,10 @@ def parse_req_from_line(name, line_source):
if spec_str.endswith(']'):
msg = f"Extras after version '{spec_str}'."
raise InstallationError(msg)
return req
if req_as_string is not None:
req = _parse_req_string(req_as_string) # type: Optional[Requirement]
else:
req = None

View file

@ -349,7 +349,7 @@ class InstallRequirement:
# When parallel builds are enabled, add a UUID to the build directory
# name so multiple builds do not interfere with each other.
dir_name = canonicalize_name(self.name)
dir_name = canonicalize_name(self.name) # type: str
if parallel_builds:
dir_name = f"{dir_name}_{uuid.uuid4().hex}"

View file

@ -28,7 +28,7 @@ class RequirementSet:
# type: () -> str
requirements = sorted(
(req for req in self.requirements.values() if not req.comes_from),
key=lambda req: canonicalize_name(req.name),
key=lambda req: canonicalize_name(req.name or ""),
)
return ' '.join(str(req.req) for req in requirements)
@ -36,7 +36,7 @@ class RequirementSet:
# type: () -> str
requirements = sorted(
self.requirements.values(),
key=lambda req: canonicalize_name(req.name),
key=lambda req: canonicalize_name(req.name or ""),
)
format_string = '<{classname} object; {count} requirement(s): {reqs}>'
@ -122,6 +122,8 @@ class RequirementSet:
existing_req and
not existing_req.constraint and
existing_req.extras == install_req.extras and
existing_req.req and
install_req.req and
existing_req.req.specifier != install_req.req.specifier
)
if has_conflicting_requirement:

View file

@ -1,14 +1,15 @@
from typing import FrozenSet, Iterable, Optional, Tuple
from typing import FrozenSet, Iterable, Optional, Tuple, Union
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import LegacyVersion, Version
from pip._internal.models.link import Link
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.hashes import Hashes
CandidateLookup = Tuple[Optional["Candidate"], Optional[InstallRequirement]]
CandidateVersion = Union[LegacyVersion, Version]
def format_name(project, extras):
@ -62,7 +63,7 @@ class Constraint:
class Requirement:
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
"""The "project name" of a requirement.
This is different from ``name`` if this requirement contains extras,
@ -97,7 +98,7 @@ class Requirement:
class Candidate:
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
"""The "project name" of the candidate.
This is different from ``name`` if this candidate contains extras,
@ -118,7 +119,7 @@ class Candidate:
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> CandidateVersion
raise NotImplementedError("Override in subclass")
@property

View file

@ -1,10 +1,10 @@
import logging
import sys
from typing import TYPE_CHECKING, Any, FrozenSet, Iterable, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, FrozenSet, Iterable, Optional, Tuple, Union, cast
from pip._vendor.packaging.specifiers import InvalidSpecifier, SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version, _BaseVersion
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import Version
from pip._vendor.packaging.version import parse as parse_version
from pip._vendor.pkg_resources import Distribution
@ -19,7 +19,7 @@ from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.misc import dist_is_editable, normalize_version_info
from pip._internal.utils.packaging import get_requires_python
from .base import Candidate, Requirement, format_name
from .base import Candidate, CandidateVersion, Requirement, format_name
if TYPE_CHECKING:
from .factory import Factory
@ -126,8 +126,8 @@ class _InstallRequirementBackedCandidate(Candidate):
source_link, # type: Link
ireq, # type: InstallRequirement
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
name=None, # type: Optional[NormalizedName]
version=None, # type: Optional[CandidateVersion]
):
# type: (...) -> None
self._link = link
@ -166,7 +166,7 @@ class _InstallRequirementBackedCandidate(Candidate):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
"""The normalised name of the project the candidate refers to"""
if self._name is None:
self._name = canonicalize_name(self.dist.project_name)
@ -179,7 +179,7 @@ class _InstallRequirementBackedCandidate(Candidate):
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> CandidateVersion
if self._version is None:
self._version = parse_version(self.dist.version)
return self._version
@ -262,8 +262,8 @@ class LinkCandidate(_InstallRequirementBackedCandidate):
link, # type: Link
template, # type: InstallRequirement
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
name=None, # type: Optional[NormalizedName]
version=None, # type: Optional[CandidateVersion]
):
# type: (...) -> None
source_link = link
@ -315,8 +315,8 @@ class EditableCandidate(_InstallRequirementBackedCandidate):
link, # type: Link
template, # type: InstallRequirement
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
name=None, # type: Optional[NormalizedName]
version=None, # type: Optional[CandidateVersion]
):
# type: (...) -> None
super().__init__(
@ -378,7 +378,7 @@ class AlreadyInstalledCandidate(Candidate):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
return canonicalize_name(self.dist.project_name)
@property
@ -388,7 +388,7 @@ class AlreadyInstalledCandidate(Candidate):
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> CandidateVersion
return parse_version(self.dist.version)
@property
@ -471,7 +471,7 @@ class ExtrasCandidate(Candidate):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
return self.base.project_name
@property
@ -482,7 +482,7 @@ class ExtrasCandidate(Candidate):
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> CandidateVersion
return self.base.version
def format_for_error(self):
@ -565,9 +565,9 @@ class RequiresPythonCandidate(Candidate):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
# Avoid conflicting with the PyPI package "Python".
return "<Python from Requires-Python>"
return cast(NormalizedName, "<Python from Requires-Python>")
@property
def name(self):
@ -576,7 +576,7 @@ class RequiresPythonCandidate(Candidate):
@property
def version(self):
# type: () -> _BaseVersion
# type: () -> CandidateVersion
return self._version
def format_for_error(self):

View file

@ -15,8 +15,7 @@ from typing import (
)
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.pkg_resources import Distribution
from pip._vendor.resolvelib import ResolutionImpossible
@ -44,7 +43,7 @@ from pip._internal.utils.misc import (
)
from pip._internal.utils.virtualenv import running_under_virtualenv
from .base import Candidate, Constraint, Requirement
from .base import Candidate, CandidateVersion, Constraint, Requirement
from .candidates import (
AlreadyInstalledCandidate,
BaseCandidate,
@ -139,8 +138,8 @@ class Factory:
link, # type: Link
extras, # type: FrozenSet[str]
template, # type: InstallRequirement
name, # type: Optional[str]
version, # type: Optional[_BaseVersion]
name, # type: Optional[NormalizedName]
version, # type: Optional[CandidateVersion]
):
# type: (...) -> Optional[Candidate]
# TODO: Check already installed candidate, and use it if the link and
@ -202,10 +201,12 @@ class Factory:
# all of them.
# Hopefully the Project model can correct this mismatch in the future.
template = ireqs[0]
assert template.req, "Candidates found on index must be PEP 508"
name = canonicalize_name(template.req.name)
extras = frozenset() # type: FrozenSet[str]
for ireq in ireqs:
assert ireq.req, "Candidates found on index must be PEP 508"
specifier &= ireq.req.specifier
hashes &= ireq.hashes(trust_internet=False)
extras |= frozenset(ireq.extras)
@ -368,7 +369,7 @@ class Factory:
def get_dist_to_uninstall(self, candidate):
# type: (Candidate) -> Optional[Distribution]
# TODO: Are there more cases this needs to return True? Editable?
dist = self._installed_dists.get(candidate.name)
dist = self._installed_dists.get(candidate.project_name)
if dist is None: # Not installed, no uninstallation required.
return None

View file

@ -1,5 +1,5 @@
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._internal.req.req_install import InstallRequirement
@ -24,7 +24,7 @@ class ExplicitRequirement(Requirement):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
# No need to canonicalise - the candidate did this
return self.candidate.project_name
@ -67,7 +67,8 @@ class SpecifierRequirement(Requirement):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
assert self._ireq.req, "Specifier-backed ireq is always PEP 508"
return canonicalize_name(self._ireq.req.name)
@property
@ -96,14 +97,14 @@ class SpecifierRequirement(Requirement):
def is_satisfied_by(self, candidate):
# type: (Candidate) -> bool
assert (
candidate.name == self.name
), "Internal issue: Candidate is not for this requirement " " {} vs {}".format(
candidate.name, self.name
assert candidate.name == self.name, (
f"Internal issue: Candidate is not for this requirement "
f"{candidate.name} vs {self.name}"
)
# We can safely always allow prereleases here since PackageFinder
# already implements the prerelease logic, and would have filtered out
# prerelease candidates if the user does not expect them.
assert self._ireq.req, "Specifier-backed ireq is always PEP 508"
spec = self._ireq.req.specifier
return spec.contains(candidate.version, prereleases=True)
@ -129,7 +130,7 @@ class RequiresPythonRequirement(Requirement):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
return self._candidate.project_name
@property
@ -160,7 +161,7 @@ class UnsatisfiableRequirement(Requirement):
"""A requirement that cannot be satisfied."""
def __init__(self, name):
# type: (str) -> None
# type: (NormalizedName) -> None
self._name = name
def __str__(self):
@ -176,7 +177,7 @@ class UnsatisfiableRequirement(Requirement):
@property
def project_name(self):
# type: () -> str
# type: () -> NormalizedName
return self._name
@property

View file

@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import parse as parse_version
from pip._vendor.resolvelib import ResolutionImpossible
from pip._vendor.resolvelib import BaseReporter, ResolutionImpossible
from pip._vendor.resolvelib import Resolver as RLResolver
from pip._vendor.resolvelib.resolvers import Result
@ -32,7 +32,7 @@ from .base import Constraint
from .factory import Factory
if TYPE_CHECKING:
from pip._vendor.resolvelib.structs import Graph
from pip._vendor.resolvelib.structs import DirectedGraph
logger = logging.getLogger(__name__)
@ -86,6 +86,7 @@ class Resolver(BaseResolver):
raise InstallationError(problem)
if not req.match_markers():
continue
assert req.name, "Constraint must be named"
name = canonicalize_name(req.name)
if name in constraints:
constraints[name] &= req
@ -110,14 +111,14 @@ class Resolver(BaseResolver):
user_requested=user_requested,
)
if "PIP_RESOLVER_DEBUG" in os.environ:
reporter = PipDebuggingReporter()
reporter = PipDebuggingReporter() # type: BaseReporter
else:
reporter = PipReporter()
resolver = RLResolver(provider, reporter)
try:
try_to_avoid_resolution_too_deep = 2000000
self._result = resolver.resolve(
result = self._result = resolver.resolve(
requirements, max_rounds=try_to_avoid_resolution_too_deep
)
@ -126,7 +127,7 @@ class Resolver(BaseResolver):
raise error from e
req_set = RequirementSet(check_supported_wheels=check_supported_wheels)
for candidate in self._result.mapping.values():
for candidate in result.mapping.values():
ireq = candidate.get_install_requirement()
if ireq is None:
continue
@ -235,7 +236,7 @@ class Resolver(BaseResolver):
def get_topological_weights(graph, expected_node_count):
# type: (Graph, int) -> Dict[Optional[str], int]
# type: (DirectedGraph, int) -> Dict[Optional[str], int]
"""Assign weights to each node based on how "deep" they are.
This implementation may change at any point in the future without prior

View file

@ -167,7 +167,7 @@ def _always_true(_):
def _verify_one(req, wheel_path):
# type: (InstallRequirement, str) -> None
canonical_name = canonicalize_name(req.name)
canonical_name = canonicalize_name(req.name or "")
w = Wheel(os.path.basename(wheel_path))
if canonicalize_name(w.name) != canonical_name:
raise InvalidWheelFilename(
@ -175,10 +175,11 @@ def _verify_one(req, wheel_path):
"got {!r}".format(canonical_name, w.name),
)
dist = get_wheel_distribution(wheel_path, canonical_name)
if canonicalize_version(dist.version) != canonicalize_version(w.version):
dist_verstr = str(dist.version)
if canonicalize_version(dist_verstr) != canonicalize_version(w.version):
raise InvalidWheelFilename(
"Wheel has unexpected file name: expected {!r}, "
"got {!r}".format(str(dist.version), w.version),
"got {!r}".format(dist_verstr, w.version),
)
metadata_version_value = dist.metadata_version
if metadata_version_value is None:
@ -192,7 +193,7 @@ def _verify_one(req, wheel_path):
and not isinstance(dist.version, Version)):
raise UnsupportedWheel(
"Metadata 1.2 mandates PEP 440 version, "
"but {!r} is not".format(str(dist.version))
"but {!r} is not".format(dist_verstr)
)
@ -242,6 +243,7 @@ def _build_one_inside_env(
assert req.name
if req.use_pep517:
assert req.metadata_directory
assert req.pep517_backend
wheel_path = build_wheel_pep517(
name=req.name,
backend=req.pep517_backend,