Skip to content
30 changes: 17 additions & 13 deletions scripts/stubsabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ async def release_contains_py_typed(release_to_download: PypiReleaseDownload, *,
raise AssertionError(f"Unknown package type: {packagetype!r}")


async def find_first_release_with_py_typed(pypi_info: PypiInfo, *, session: aiohttp.ClientSession) -> PypiReleaseDownload:
async def find_first_release_with_py_typed(pypi_info: PypiInfo, *, session: aiohttp.ClientSession) -> PypiReleaseDownload | None:
release_iter = pypi_info.releases_in_descending_order()
# If the latest release is not py.typed, assume none are.
if not (await release_contains_py_typed(release := next(release_iter), session=session)):
return None

first_release_with_py_typed: PypiReleaseDownload | None = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not change the signature of this method and just have it assert that the return value is not None. This really should only be called with the precondition that release_contains_py_typed was True, otherwise we'll just download all packages for all time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively I'd also be fine with moving release_contains_py_typed into here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like 85a2d26 (#9754) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, sorry I read a thing wrong, your change was fine as is!

while await release_contains_py_typed(release := next(release_iter), session=session):
if not release.version.is_prerelease:
first_release_with_py_typed = release
Expand Down Expand Up @@ -418,12 +423,8 @@ async def determine_action(stub_path: Path, session: aiohttp.ClientSession) -> U
if latest_version in spec:
return NoUpdate(stub_info.distribution, "up to date")

is_obsolete = await release_contains_py_typed(latest_release, session=session)
if is_obsolete:
first_release_with_py_typed = await find_first_release_with_py_typed(pypi_info, session=session)
relevant_version = version_obsolete_since = first_release_with_py_typed.version
else:
relevant_version = latest_version
obsolete_since = await find_first_release_with_py_typed(pypi_info, session=session)
relevant_version = obsolete_since.version if obsolete_since else latest_version

project_urls = pypi_info.info["project_urls"] or {}
maybe_links: dict[str, str | None] = {
Expand All @@ -435,23 +436,26 @@ async def determine_action(stub_path: Path, session: aiohttp.ClientSession) -> U

diff_info = await get_diff_info(session, stub_info, pypi_info, relevant_version)
if diff_info is not None:
github_repo_path, old_tag, new_tag, diff_url = diff_info
links["Diff"] = diff_url
links["Diff"] = diff_info.diff_url

if is_obsolete:
if obsolete_since:
return Obsolete(
stub_info.distribution,
stub_path,
obsolete_since_version=str(version_obsolete_since),
obsolete_since_date=first_release_with_py_typed.upload_date,
obsolete_since_version=str(obsolete_since.version),
obsolete_since_date=obsolete_since.upload_date,
links=links,
)

if diff_info is None:
diff_analysis: DiffAnalysis | None = None
else:
diff_analysis = await analyze_diff(
github_repo_path=github_repo_path, stub_path=stub_path, old_tag=old_tag, new_tag=new_tag, session=session
github_repo_path=diff_info.repo_path,
stub_path=stub_path,
old_tag=diff_info.old_tag,
new_tag=diff_info.new_tag,
session=session,
)

return Update(
Expand Down