Skip to content

Commit 70eab89

Browse files
authored
Don't restrict version numbers to two digits (#30)
Extract fetch_pypi_versions()
1 parent 4edb981 commit 70eab89

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

scripts/get_version.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
distribution information.
1111
"""
1212

13+
from __future__ import annotations
14+
1315
import argparse
1416
import os.path
15-
from typing import Optional, cast
17+
from collections.abc import Iterable
18+
from typing import Any, Optional, cast
1619

1720
import requests
1821
import toml
@@ -28,6 +31,21 @@
2831
TIMEOUT = 3
2932

3033

34+
def fetch_pypi_versions(distribution: str) -> Iterable[str]:
35+
url = URL_TEMPLATE.format(PREFIX + distribution)
36+
retry_strategy = Retry(total=RETRIES, status_forcelist=RETRY_ON)
37+
with requests.Session() as session:
38+
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
39+
resp = session.get(url, timeout=TIMEOUT)
40+
if not resp.ok:
41+
if resp.status_code == 404:
42+
# Looks like this is first time this package is ever uploaded.
43+
return []
44+
raise ValueError("Error while retrieving version")
45+
releases: dict[str, Any] = resp.json()["releases"]
46+
return releases.keys()
47+
48+
3149
def read_base_version(typeshed_dir: str, distribution: str) -> str:
3250
"""Read distribution version from metadata."""
3351
metadata_file = os.path.join(
@@ -67,25 +85,13 @@ def main(typeshed_dir: str, distribution: str, version: Optional[str]) -> int:
6785
6886
Supports basic reties and timeouts (as module constants).
6987
"""
70-
url = URL_TEMPLATE.format(PREFIX + distribution)
71-
retry_strategy = Retry(total=RETRIES, status_forcelist=RETRY_ON)
72-
with requests.Session() as session:
73-
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
74-
resp = session.get(url, timeout=TIMEOUT)
75-
if not resp.ok:
76-
if resp.status_code == 404:
77-
# Looks like this is first time this package is ever uploaded.
78-
return -1
79-
raise ValueError("Error while retrieving version")
80-
data = resp.json()
88+
pypi_versions = fetch_pypi_versions(distribution)
8189
if not version:
8290
# Use the METADATA.toml version, if not given one.
8391
version = read_base_version(typeshed_dir, distribution)
84-
assert version.count(".") == 1
85-
matching = [v for v in data["releases"].keys() if v.startswith(version)]
92+
matching = [v for v in pypi_versions if v.startswith(f"{version}.")]
8693
if not matching:
8794
return -1
88-
assert all(v.count(".") == 2 for v in matching)
8995
increment = max(int(v.split(".")[-1]) for v in matching)
9096
return increment
9197

0 commit comments

Comments
 (0)