Skip to content

Commit

Permalink
refactor(repository): always raise PackageNotFound in _get_page inste…
Browse files Browse the repository at this point in the history
…ad of returning None; remove unsuccessful attempt to get pretty name via json-based simple API
  • Loading branch information
radoering authored and neersighted committed Jan 1, 2023
1 parent a013c5c commit bfd490c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/poetry/repositories/http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 8 additions & 14 deletions src/poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 []

Expand All @@ -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)
Expand All @@ -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)
14 changes: 1 addition & 13 deletions src/poetry/repositories/pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,14 @@ 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))
for version in json_page.versions(name)
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"}
Expand Down
5 changes: 3 additions & 2 deletions src/poetry/repositories/single_page_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
9 changes: 5 additions & 4 deletions tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 0 additions & 8 deletions tests/repositories/test_pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
5 changes: 3 additions & 2 deletions tests/repositories/test_single_page_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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())
Expand Down

0 comments on commit bfd490c

Please sign in to comment.