From b26455f2261ac0e3456065e30a7e386dc0d5195f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Thu, 22 Dec 2022 14:28:44 +0100 Subject: [PATCH] refactor(repository): always raise PackageNotFound in _get_page instead of returning None; remove unsuccessful attempt to get pretty name via json-based simple API --- src/poetry/repositories/http_repository.py | 4 ++-- src/poetry/repositories/legacy_repository.py | 22 +++++++------------ src/poetry/repositories/pypi_repository.py | 14 +----------- .../repositories/single_page_repository.py | 5 +++-- tests/repositories/test_legacy_repository.py | 9 ++++---- tests/repositories/test_pypi_repository.py | 8 ------- .../test_single_page_repository.py | 5 +++-- 7 files changed, 22 insertions(+), 45 deletions(-) diff --git a/src/poetry/repositories/http_repository.py b/src/poetry/repositories/http_repository.py index 8077f9ce57e..298fc1e815b 100644 --- a/src/poetry/repositories/http_repository.py +++ b/src/poetry/repositories/http_repository.py @@ -296,8 +296,8 @@ def _get_response(self, endpoint: str) -> requests.Response | None: ) return response - def _get_page(self, name: NormalizedName) -> LinkSource | None: + def _get_page(self, name: NormalizedName) -> LinkSource: response = self._get_response(f"/{name}/") if not response: - return None + raise PackageNotFound(f"Package [{name}] not found.") return HTMLPage(response.url, response.text) diff --git a/src/poetry/repositories/legacy_repository.py b/src/poetry/repositories/legacy_repository.py index 89e5221a706..7654d64d921 100644 --- a/src/poetry/repositories/legacy_repository.py +++ b/src/poetry/repositories/legacy_repository.py @@ -72,8 +72,9 @@ def package( return package def find_links_for_package(self, package: Package) -> list[Link]: - page = self.get_page(package.name) - if page is None: + try: + page = self.get_page(package.name) + except PackageNotFound: return [] return list(page.links_for_version(package.name, package.version)) @@ -84,14 +85,9 @@ def _find_packages( """ Find packages on the remote server. """ - versions: list[tuple[Version, str | bool]] - - key: str = name - if not constraint.is_any(): - key = f"{key}:{constraint!s}" - - page = self.get_page(name) - if page is None: + try: + page = self.get_page(name) + except PackageNotFound: self._log(f"No packages found for {name}", level="debug") return [] @@ -117,8 +113,6 @@ def _get_release_info( self, name: NormalizedName, version: Version ) -> dict[str, Any]: page = self.get_page(name) - if page is None: - raise PackageNotFound(f'No package named "{name}"') links = list(page.links_for_version(name, version)) yanked = page.yanked(name, version) @@ -138,8 +132,8 @@ def _get_release_info( ), ) - def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage | None: + def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage: response = self._get_response(f"/{name}/") if not response: - return None + raise PackageNotFound(f"Package [{name}] not found.") return SimpleRepositoryPage(response.url, response.text) diff --git a/src/poetry/repositories/pypi_repository.py b/src/poetry/repositories/pypi_repository.py index 35c8c99e1ca..14f0b0fc3f8 100644 --- a/src/poetry/repositories/pypi_repository.py +++ b/src/poetry/repositories/pypi_repository.py @@ -115,13 +115,6 @@ def _find_packages( except PackageNotFound: self._log(f"No packages found for {name}", level="debug") return [] - assert isinstance(json_page, SimpleJsonPage) - - versions: list[tuple[Version, str | bool]] - - key: str = name - if not constraint.is_any(): - key = f"{key}:{constraint!s}" versions = [ (version, json_page.yanked(name, version)) @@ -129,12 +122,7 @@ def _find_packages( if constraint.allows(version) ] - pretty_name = json_page.content["name"] - packages = [ - Package(pretty_name, version, yanked=yanked) for version, yanked in versions - ] - - return packages + return [Package(name, version, yanked=yanked) for version, yanked in versions] def _get_package_info(self, name: str) -> dict[str, Any]: headers = {"Accept": "application/vnd.pypi.simple.v1+json"} diff --git a/src/poetry/repositories/single_page_repository.py b/src/poetry/repositories/single_page_repository.py index 1cd77a4d3a1..7bdc469bbf0 100644 --- a/src/poetry/repositories/single_page_repository.py +++ b/src/poetry/repositories/single_page_repository.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING +from poetry.repositories.exceptions import PackageNotFound from poetry.repositories.legacy_repository import LegacyRepository from poetry.repositories.link_sources.html import SimpleRepositoryPage @@ -11,11 +12,11 @@ class SinglePageRepository(LegacyRepository): - def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage | None: + def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage: """ Single page repositories only have one page irrespective of endpoint. """ response = self._get_response("") if not response: - return None + raise PackageNotFound(f"Package [{name}] not found.") return SimpleRepositoryPage(response.url, response.text) diff --git a/tests/repositories/test_legacy_repository.py b/tests/repositories/test_legacy_repository.py index c17f42047fc..bdb68935894 100644 --- a/tests/repositories/test_legacy_repository.py +++ b/tests/repositories/test_legacy_repository.py @@ -46,10 +46,10 @@ class MockRepository(LegacyRepository): def __init__(self) -> None: super().__init__("legacy", url="http://legacy.foo.bar", disable_cache=True) - def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage | None: + def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage: fixture = self.FIXTURES / (name + ".html") if not fixture.exists(): - return None + raise PackageNotFound(f"Package [{name}] not found.") with fixture.open(encoding="utf-8") as f: return SimpleRepositoryPage(self._url + f"/{name}/", f.read()) @@ -432,7 +432,7 @@ def test_package_yanked( def test_package_partial_yank(): class SpecialMockRepository(MockRepository): - def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage | None: + def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage: return super()._get_page(canonicalize_name(f"{name}-partial-yank")) repo = MockRepository() @@ -490,7 +490,8 @@ def test_get_40x_and_returns_none( ) -> None: repo = MockHttpRepository({"/foo/": status_code}, http) - assert repo.get_page("foo") is None + with pytest.raises(PackageNotFound): + repo.get_page("foo") def test_get_5xx_raises(http: type[httpretty.httpretty]) -> None: diff --git a/tests/repositories/test_pypi_repository.py b/tests/repositories/test_pypi_repository.py index 5ab69c77f2b..bff1a4fb5d5 100644 --- a/tests/repositories/test_pypi_repository.py +++ b/tests/repositories/test_pypi_repository.py @@ -332,14 +332,6 @@ def test_urls() -> None: assert repository.authenticated_url == "https://pypi.org/simple/" -def test_use_pypi_pretty_name() -> None: - repo = MockRepository(fallback=True) - - package = repo.find_packages(Factory.create_dependency("twisted", "*")) - assert len(package) == 1 - assert package[0].pretty_name == "Twisted" - - def test_find_links_for_package_of_supported_types(): repo = MockRepository() package = repo.find_packages(Factory.create_dependency("hbmqtt", "0.9.6")) diff --git a/tests/repositories/test_single_page_repository.py b/tests/repositories/test_single_page_repository.py index 081a58d6f32..804fa3d0125 100644 --- a/tests/repositories/test_single_page_repository.py +++ b/tests/repositories/test_single_page_repository.py @@ -7,6 +7,7 @@ from poetry.core.packages.dependency import Dependency +from poetry.repositories.exceptions import PackageNotFound from poetry.repositories.link_sources.html import SimpleRepositoryPage from poetry.repositories.single_page_repository import SinglePageRepository @@ -25,10 +26,10 @@ def __init__(self, page: str) -> None: disable_cache=True, ) - def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage | None: + def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage: fixture = self.FIXTURES / self.url.rsplit("/", 1)[-1] if not fixture.exists(): - return + raise PackageNotFound(f"Package [{name}] not found.") with fixture.open(encoding="utf-8") as f: return SimpleRepositoryPage(self._url, f.read())