Skip to content

Commit f19f634

Browse files
hugovkAA-Turner
andauthored
Update versions in README.rst (#301)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 6ef43b0 commit f19f634

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

release.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import subprocess
2121
import sys
2222
import tempfile
23+
import urllib.request
2324
from collections.abc import Callable, Generator, Sequence
2425
from contextlib import contextmanager
2526
from dataclasses import dataclass
27+
from functools import cache
2628
from pathlib import Path
2729
from typing import (
2830
Any,
@@ -475,6 +477,22 @@ def tweak_patchlevel(
475477
print("done")
476478

477479

480+
@cache
481+
def get_pep_number(version: str) -> str:
482+
"""Fetch PEP number for a Python version from peps.python.org.
483+
484+
Returns the PEP number as a string, or "TODO" if not found.
485+
"""
486+
url = "https://peps.python.org/api/release-cycle.json"
487+
with urllib.request.urlopen(url, timeout=10) as response:
488+
data = json.loads(response.read().decode())
489+
if version in data:
490+
pep = data[version].get("pep")
491+
if pep:
492+
return str(pep)
493+
return "TODO"
494+
495+
478496
def tweak_readme(tag: Tag, filename: str = "README.rst") -> None:
479497
print(f"Updating {filename}...", end=" ")
480498
readme = Path(filename)
@@ -486,8 +504,37 @@ def tweak_readme(tag: Tag, filename: str = "README.rst") -> None:
486504
underline = "=" * len(this_is)
487505
lines[0] = this_is
488506
lines[1] = underline
507+
content = "\n".join(lines)
508+
509+
DOCS_URL = r"https://docs\.python\.org/"
510+
X_Y = r"\d+\.\d+"
511+
512+
# Replace in: 3.14 <https://docs.python.org/3.14/whatsnew/3.14.html>`_
513+
content = re.sub(
514+
rf"{X_Y} (<{DOCS_URL}){X_Y}(/whatsnew/){X_Y}(\.html>`_)",
515+
rf"{tag.basic_version} \g<1>{tag.basic_version}\g<2>{tag.basic_version}\g<3>",
516+
content,
517+
)
518+
519+
# Replace in: `Documentation for Python 3.14 <https://docs.python.org/3.14/>`_
520+
content = re.sub(
521+
rf"(`Documentation for Python ){X_Y}( <{DOCS_URL}){X_Y}(/>`_)",
522+
rf"\g<1>{tag.basic_version}\g<2>{tag.basic_version}\g<3>",
523+
content,
524+
)
525+
526+
# Get PEP number for this version
527+
pep_number = get_pep_number(tag.basic_version)
528+
pep_padded = pep_number.zfill(4) if pep_number != "TODO" else "TODO"
529+
530+
# Replace in: `PEP 745 <https://peps.python.org/pep-0745/>`__ for Python 3.14
531+
content = re.sub(
532+
rf"(`PEP )\d+( <https://peps\.python\.org/pep-)\d+(/>`__ for Python ){X_Y}",
533+
rf"\g<1>{pep_number}\g<2>{pep_padded}\g<3>{tag.basic_version}",
534+
content,
535+
)
489536

490-
readme.write_text("\n".join(lines))
537+
readme.write_text(content)
491538
print("done")
492539

493540

tests/test_release.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,57 @@ def test_tweak_patchlevel(tmp_path: Path) -> None:
103103

104104

105105
@pytest.mark.parametrize(
106-
["test_tag", "expected_version", "expected_underline"],
106+
[
107+
"test_tag",
108+
"expected_version",
109+
"expected_underline",
110+
"expected_whatsnew",
111+
"expected_docs",
112+
"expected_pep_line",
113+
],
107114
[
108115
(
109116
"3.14.0a6",
110117
"This is Python version 3.14.0 alpha 6",
111118
"=====================================",
119+
"3.14 <https://docs.python.org/3.14/whatsnew/3.14.html>`_",
120+
"`Documentation for Python 3.14 <https://docs.python.org/3.14/>`_",
121+
"`PEP 745 <https://peps.python.org/pep-0745/>`__ for Python 3.14",
112122
),
113123
(
114124
"3.14.0b2",
115125
"This is Python version 3.14.0 beta 2",
116126
"====================================",
127+
"3.14 <https://docs.python.org/3.14/whatsnew/3.14.html>`_",
128+
"`Documentation for Python 3.14 <https://docs.python.org/3.14/>`_",
129+
"`PEP 745 <https://peps.python.org/pep-0745/>`__ for Python 3.14",
117130
),
118131
(
119132
"3.14.0rc2",
120133
"This is Python version 3.14.0 release candidate 2",
121134
"=================================================",
135+
"3.14 <https://docs.python.org/3.14/whatsnew/3.14.html>`_",
136+
"`Documentation for Python 3.14 <https://docs.python.org/3.14/>`_",
137+
"`PEP 745 <https://peps.python.org/pep-0745/>`__ for Python 3.14",
122138
),
123139
(
124-
"3.14.1",
125-
"This is Python version 3.14.1",
140+
"3.15.1",
141+
"This is Python version 3.15.1",
126142
"=============================",
143+
"3.15 <https://docs.python.org/3.15/whatsnew/3.15.html>`_",
144+
"`Documentation for Python 3.15 <https://docs.python.org/3.15/>`_",
145+
"`PEP 790 <https://peps.python.org/pep-0790/>`__ for Python 3.15",
127146
),
128147
],
129148
)
130149
def test_tweak_readme(
131-
tmp_path: Path, test_tag: str, expected_version: str, expected_underline: str
150+
tmp_path: Path,
151+
test_tag: str,
152+
expected_version: str,
153+
expected_underline: str,
154+
expected_whatsnew: str,
155+
expected_docs: str,
156+
expected_pep_line: str,
132157
) -> None:
133158
# Arrange
134159
tag = release.Tag(test_tag)
@@ -142,11 +167,12 @@ def test_tweak_readme(
142167
release.tweak_readme(tag, filename=str(readme_file))
143168

144169
# Assert
145-
original_lines = original_contents.split("\n")
146170
new_contents = readme_file.read_text()
147171
new_lines = new_contents.split("\n")
148172
assert new_lines[0] == expected_version
149173
assert new_lines[1] == expected_underline
150-
assert new_lines[2:] == original_lines[2:]
174+
assert expected_whatsnew in new_contents
175+
assert expected_docs in new_contents
176+
assert expected_pep_line in new_contents
151177
assert original_contents.endswith("\n")
152178
assert new_contents.endswith("\n")

0 commit comments

Comments
 (0)