diff options
author | Tzu-ping Chung <uranusjr@gmail.com> | 2021-12-05 04:58:04 +0800 |
---|---|---|
committer | Tzu-ping Chung <uranusjr@gmail.com> | 2022-04-12 03:26:11 +0800 |
commit | d72356dc64565b1b7a245e7eb5bc813296b04b95 (patch) | |
tree | 30a029dfccb301414b0b08426fe4c3970c7155c2 | |
parent | f13fa41bb3c8cff42b2b2c98c4d9d3f17a1a39a4 (diff) | |
download | pip-d72356dc64565b1b7a245e7eb5bc813296b04b95.tar.gz |
Hoist distrubution factory methods to base class
-rw-r--r-- | src/pip/_internal/metadata/base.py | 22 | ||||
-rw-r--r-- | src/pip/_internal/metadata/pkg_resources.py | 11 |
2 files changed, 24 insertions, 9 deletions
diff --git a/src/pip/_internal/metadata/base.py b/src/pip/_internal/metadata/base.py index 7586dfc46..2d6bb1cc8 100644 --- a/src/pip/_internal/metadata/base.py +++ b/src/pip/_internal/metadata/base.py @@ -96,6 +96,28 @@ def _convert_installed_files_path( class BaseDistribution(Protocol): + @classmethod + def from_directory(cls, directory: str) -> "BaseDistribution": + """Load the distribution from a metadata directory. + + :param directory: Path to a metadata directory, e.g. ``.dist-info``. + """ + raise NotImplementedError() + + @classmethod + def from_wheel(cls, wheel: "Wheel", name: str) -> "BaseDistribution": + """Load the distribution from a given wheel. + + :param wheel: A concrete wheel definition. + :param name: File name of the wheel. + + :raises InvalidWheel: Whenever loading of the wheel causes a + :py:exc:`zipfile.BadZipFile` exception to be thrown. + :raises UnsupportedWheel: If the wheel is a valid zip, but malformed + internally. + """ + raise NotImplementedError() + def __repr__(self) -> str: return f"{self.raw_name} {self.version} ({self.location})" diff --git a/src/pip/_internal/metadata/pkg_resources.py b/src/pip/_internal/metadata/pkg_resources.py index 0e56f98e2..667fbb936 100644 --- a/src/pip/_internal/metadata/pkg_resources.py +++ b/src/pip/_internal/metadata/pkg_resources.py @@ -73,7 +73,7 @@ class Distribution(BaseDistribution): self._dist = dist @classmethod - def from_directory(cls, directory: str) -> "Distribution": + def from_directory(cls, directory: str) -> BaseDistribution: dist_dir = directory.rstrip(os.sep) # Build a PathMetadata object, from path to metadata. :wink: @@ -93,14 +93,7 @@ class Distribution(BaseDistribution): return cls(dist) @classmethod - def from_wheel(cls, wheel: Wheel, name: str) -> "Distribution": - """Load the distribution from a given wheel. - - :raises InvalidWheel: Whenever loading of the wheel causes a - :py:exc:`zipfile.BadZipFile` exception to be thrown. - :raises UnsupportedWheel: If the wheel is a valid zip, but malformed - internally. - """ + def from_wheel(cls, wheel: Wheel, name: str) -> BaseDistribution: try: with wheel.as_zipfile() as zf: info_dir, _ = parse_wheel(zf, name) |