mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
Complete type annotations in pip/_internal/metadata
(#10124)
This commit is contained in:
parent
156f71b5e8
commit
9582341e08
1
news/10124.trivial.rst
Normal file
1
news/10124.trivial.rst
Normal file
|
@ -0,0 +1 @@
|
|||
Converted type commentaries into annotations in ``pip/_internal/metadata``.
|
|
@ -3,8 +3,7 @@ from typing import List, Optional
|
|||
from .base import BaseDistribution, BaseEnvironment
|
||||
|
||||
|
||||
def get_default_environment():
|
||||
# type: () -> BaseEnvironment
|
||||
def get_default_environment() -> BaseEnvironment:
|
||||
"""Get the default representation for the current environment.
|
||||
|
||||
This returns an Environment instance from the chosen backend. The default
|
||||
|
@ -16,8 +15,7 @@ def get_default_environment():
|
|||
return Environment.default()
|
||||
|
||||
|
||||
def get_environment(paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
def get_environment(paths: Optional[List[str]]) -> BaseEnvironment:
|
||||
"""Get a representation of the environment specified by ``paths``.
|
||||
|
||||
This returns an Environment instance from the chosen backend based on the
|
||||
|
@ -29,8 +27,7 @@ def get_environment(paths):
|
|||
return Environment.from_paths(paths)
|
||||
|
||||
|
||||
def get_wheel_distribution(wheel_path, canonical_name):
|
||||
# type: (str, str) -> BaseDistribution
|
||||
def get_wheel_distribution(wheel_path: str, canonical_name: str) -> BaseDistribution:
|
||||
"""Get the representation of the specified wheel's distribution metadata.
|
||||
|
||||
This returns a Distribution instance from the chosen backend based on
|
||||
|
|
|
@ -13,8 +13,7 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
class BaseDistribution:
|
||||
@property
|
||||
def location(self):
|
||||
# type: () -> Optional[str]
|
||||
def location(self) -> Optional[str]:
|
||||
"""Where the distribution is loaded from.
|
||||
|
||||
A string value is not necessarily a filesystem path, since distributions
|
||||
|
@ -24,39 +23,32 @@ class BaseDistribution:
|
|||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def metadata_version(self):
|
||||
# type: () -> Optional[str]
|
||||
def metadata_version(self) -> Optional[str]:
|
||||
"""Value of "Metadata-Version:" in the distribution, if available."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def canonical_name(self):
|
||||
# type: () -> str
|
||||
def canonical_name(self) -> str:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
# type: () -> DistributionVersion
|
||||
def version(self) -> DistributionVersion:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def installer(self):
|
||||
# type: () -> str
|
||||
def installer(self) -> str:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def editable(self):
|
||||
# type: () -> bool
|
||||
def editable(self) -> bool:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def local(self):
|
||||
# type: () -> bool
|
||||
def local(self) -> bool:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def in_usersite(self):
|
||||
# type: () -> bool
|
||||
def in_usersite(self) -> bool:
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
|
@ -64,22 +56,18 @@ class BaseEnvironment:
|
|||
"""An environment containing distributions to introspect."""
|
||||
|
||||
@classmethod
|
||||
def default(cls):
|
||||
# type: () -> BaseEnvironment
|
||||
def default(cls) -> "BaseEnvironment":
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def from_paths(cls, paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
def from_paths(cls, paths: Optional[List[str]]) -> "BaseEnvironment":
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_distribution(self, name):
|
||||
# type: (str) -> Optional[BaseDistribution]
|
||||
def get_distribution(self, name: str) -> Optional["BaseDistribution"]:
|
||||
"""Given a requirement name, return the installed distributions."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
def _iter_distributions(self) -> Iterator["BaseDistribution"]:
|
||||
"""Iterate through installed distributions.
|
||||
|
||||
This function should be implemented by subclass, but never called
|
||||
|
@ -88,8 +76,7 @@ class BaseEnvironment:
|
|||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
def iter_distributions(self) -> Iterator["BaseDistribution"]:
|
||||
"""Iterate through installed distributions."""
|
||||
for dist in self._iter_distributions():
|
||||
# Make sure the distribution actually comes from a valid Python
|
||||
|
@ -112,13 +99,12 @@ class BaseEnvironment:
|
|||
|
||||
def iter_installed_distributions(
|
||||
self,
|
||||
local_only=True, # type: bool
|
||||
skip=stdlib_pkgs, # type: Container[str]
|
||||
include_editables=True, # type: bool
|
||||
editables_only=False, # type: bool
|
||||
user_only=False, # type: bool
|
||||
):
|
||||
# type: (...) -> Iterator[BaseDistribution]
|
||||
local_only: bool = True,
|
||||
skip: Container[str] = stdlib_pkgs,
|
||||
include_editables: bool = True,
|
||||
editables_only: bool = False,
|
||||
user_only: bool = False,
|
||||
) -> Iterator[BaseDistribution]:
|
||||
"""Return a list of installed distributions.
|
||||
|
||||
:param local_only: If True (default), only return installations
|
||||
|
|
|
@ -13,78 +13,64 @@ from .base import BaseDistribution, BaseEnvironment, DistributionVersion
|
|||
|
||||
|
||||
class Distribution(BaseDistribution):
|
||||
def __init__(self, dist):
|
||||
# type: (pkg_resources.Distribution) -> None
|
||||
def __init__(self, dist: pkg_resources.Distribution) -> None:
|
||||
self._dist = dist
|
||||
|
||||
@classmethod
|
||||
def from_wheel(cls, path, name):
|
||||
# type: (str, str) -> Distribution
|
||||
def from_wheel(cls, path: str, name: str) -> "Distribution":
|
||||
with zipfile.ZipFile(path, allowZip64=True) as zf:
|
||||
dist = pkg_resources_distribution_for_wheel(zf, name, path)
|
||||
return cls(dist)
|
||||
|
||||
@property
|
||||
def location(self):
|
||||
# type: () -> Optional[str]
|
||||
def location(self) -> Optional[str]:
|
||||
return self._dist.location
|
||||
|
||||
@property
|
||||
def metadata_version(self):
|
||||
# type: () -> Optional[str]
|
||||
def metadata_version(self) -> Optional[str]:
|
||||
for line in self._dist.get_metadata_lines(self._dist.PKG_INFO):
|
||||
if line.lower().startswith("metadata-version:"):
|
||||
return line.split(":", 1)[-1].strip()
|
||||
return None
|
||||
|
||||
@property
|
||||
def canonical_name(self):
|
||||
# type: () -> str
|
||||
def canonical_name(self) -> str:
|
||||
return canonicalize_name(self._dist.project_name)
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
# type: () -> DistributionVersion
|
||||
def version(self) -> DistributionVersion:
|
||||
return parse_version(self._dist.version)
|
||||
|
||||
@property
|
||||
def installer(self):
|
||||
# type: () -> str
|
||||
def installer(self) -> str:
|
||||
return get_installer(self._dist)
|
||||
|
||||
@property
|
||||
def editable(self):
|
||||
# type: () -> bool
|
||||
def editable(self) -> bool:
|
||||
return misc.dist_is_editable(self._dist)
|
||||
|
||||
@property
|
||||
def local(self):
|
||||
# type: () -> bool
|
||||
def local(self) -> bool:
|
||||
return misc.dist_is_local(self._dist)
|
||||
|
||||
@property
|
||||
def in_usersite(self):
|
||||
# type: () -> bool
|
||||
def in_usersite(self) -> bool:
|
||||
return misc.dist_in_usersite(self._dist)
|
||||
|
||||
|
||||
class Environment(BaseEnvironment):
|
||||
def __init__(self, ws):
|
||||
# type: (pkg_resources.WorkingSet) -> None
|
||||
def __init__(self, ws: pkg_resources.WorkingSet) -> None:
|
||||
self._ws = ws
|
||||
|
||||
@classmethod
|
||||
def default(cls):
|
||||
# type: () -> BaseEnvironment
|
||||
def default(cls) -> BaseEnvironment:
|
||||
return cls(pkg_resources.working_set)
|
||||
|
||||
@classmethod
|
||||
def from_paths(cls, paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
def from_paths(cls, paths: Optional[List[str]]) -> BaseEnvironment:
|
||||
return cls(pkg_resources.WorkingSet(paths))
|
||||
|
||||
def _search_distribution(self, name):
|
||||
# type: (str) -> Optional[BaseDistribution]
|
||||
def _search_distribution(self, name: str) -> Optional[BaseDistribution]:
|
||||
"""Find a distribution matching the ``name`` in the environment.
|
||||
|
||||
This searches from *all* distributions available in the environment, to
|
||||
|
@ -96,8 +82,7 @@ class Environment(BaseEnvironment):
|
|||
return dist
|
||||
return None
|
||||
|
||||
def get_distribution(self, name):
|
||||
# type: (str) -> Optional[BaseDistribution]
|
||||
def get_distribution(self, name: str) -> Optional[BaseDistribution]:
|
||||
|
||||
# Search the distribution by looking through the working set.
|
||||
dist = self._search_distribution(name)
|
||||
|
@ -120,7 +105,6 @@ class Environment(BaseEnvironment):
|
|||
return None
|
||||
return self._search_distribution(name)
|
||||
|
||||
def _iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
def _iter_distributions(self) -> Iterator[BaseDistribution]:
|
||||
for dist in self._ws:
|
||||
yield Distribution(dist)
|
||||
|
|
Loading…
Reference in a new issue