Skip to content

Commit 8ddf39e

Browse files
authored
Infra: Derive Discussions-To from Post-History (#5244)
1 parent f3b687e commit 8ddf39e

5 files changed

Lines changed: 210 additions & 3 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Shared URL handling for PEP discussion links."""
2+
3+
import re
4+
5+
6+
def discourse_topic_url(url: str) -> str:
7+
"""Remove the post number from a Python Discourse reply URL."""
8+
return re.sub(
9+
r"^(https://discuss\.python\.org/t/(?:[\w-]+/)?[0-9]+)/[0-9]+/?$",
10+
r"\1",
11+
url,
12+
)

‎pep_sphinx_extensions/pep_processor/transforms/pep_headers.py‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from docutils import nodes, transforms
55
from sphinx import errors
66

7+
from pep_sphinx_extensions.discussions import discourse_topic_url
78
from pep_sphinx_extensions.pep_processor.transforms import pep_zero
89
from pep_sphinx_extensions.pep_processor.transforms.pep_zero import _mask_email
910
from pep_sphinx_extensions.pep_zero_generator.constants import (
@@ -95,11 +96,14 @@ def apply(self) -> None:
9596
if len(header) < 2 or header[1][0].astext().lower() != "title":
9697
raise PEPParsingError("No title!")
9798

99+
generated_discussions_to = _insert_discussions_to_fallback(header)
98100
fields_to_remove = []
99101
self.document["headers"] = headers = {}
100102
for field in header:
101-
row_attributes = {sub.tagname: sub.rawsource for sub in field}
102-
headers[row_attributes["field_name"]] = row_attributes["field_body"]
103+
# Only source fields belong in the raw header metadata.
104+
if field is not generated_discussions_to:
105+
row_attributes = {sub.tagname: sub.rawsource for sub in field}
106+
headers[row_attributes["field_name"]] = row_attributes["field_body"]
103107

104108
name = field[0].astext().lower()
105109
body = field[1]
@@ -199,6 +203,40 @@ def apply(self) -> None:
199203
field.parent.remove(field)
200204

201205

206+
def _insert_discussions_to_fallback(header: nodes.field_list) -> nodes.field | None:
207+
post_history = None
208+
insert_at = len(header)
209+
for index, field in enumerate(header):
210+
name = field[0].astext().lower()
211+
if name == "discussions-to":
212+
return None
213+
if name == "post-history":
214+
post_history = field[1]
215+
elif name == "status":
216+
insert_at = index
217+
218+
if post_history is None:
219+
return None
220+
221+
urls = [
222+
reference["refuri"]
223+
for reference in post_history.findall(nodes.reference)
224+
if reference.get("refuri")
225+
]
226+
if not urls:
227+
return None
228+
229+
url = discourse_topic_url(urls[-1])
230+
link = nodes.reference("", url, refuri=url)
231+
discussion_field = nodes.field(
232+
"",
233+
nodes.field_name("", "Discussions-To"),
234+
nodes.field_body("", nodes.paragraph("", "", link)),
235+
)
236+
header.insert(insert_at, discussion_field)
237+
return discussion_field
238+
239+
202240
def _generate_list_url(mailto: str) -> str:
203241
list_name_domain = mailto.lower().removeprefix("mailto:").strip()
204242
list_name = list_name_domain.split("@")[0]

‎pep_sphinx_extensions/pep_zero_generator/parser.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from __future__ import annotations
44

55
import dataclasses
6+
import re
67
from collections.abc import Iterable, Sequence
78
from email.parser import HeaderParser
89
from pathlib import Path
910

11+
from pep_sphinx_extensions.discussions import discourse_topic_url
1012
from pep_sphinx_extensions.pep_zero_generator.constants import (
1113
ACTIVE_ALLOWED,
1214
HIDE_STATUS,
@@ -114,7 +116,6 @@ def __init__(self, filename: Path):
114116

115117
# Other headers
116118
self.created = metadata["Created"]
117-
self.discussions_to = metadata["Discussions-To"]
118119
self.python_version = metadata["Python-Version"]
119120
self.replaces = metadata["Replaces"]
120121
self.requires = metadata["Requires"]
@@ -126,6 +127,13 @@ def __init__(self, filename: Path):
126127
else:
127128
self.post_history = None
128129

130+
self.discussions_to = metadata["Discussions-To"]
131+
if self.discussions_to is None and self.post_history:
132+
# Post-History represents linked dates as `DD-Mmm-YYYY <URL>`__.
133+
urls = re.findall(r"<([^<>\s]+)>`__?", self.post_history)
134+
if urls:
135+
self.discussions_to = discourse_topic_url(urls[-1])
136+
129137
def __repr__(self) -> str:
130138
return f"<PEP {self.number:0>4} - {self.title}>"
131139

‎pep_sphinx_extensions/tests/pep_processor/transform/test_pep_headers.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import pytest
2+
from docutils import nodes
3+
from docutils.core import publish_doctree
4+
from docutils.parsers.rst import Parser
25

36
from pep_sphinx_extensions.pep_processor.transforms import pep_headers
47
from pep_sphinx_extensions.pep_zero_generator.constants import (
@@ -17,6 +20,104 @@
1720
)
1821

1922

23+
def _render_pep_headers(post_history, *, discussions_to=""):
24+
document = publish_doctree(
25+
f"PEP: 9000\nTitle: Test PEP\n{discussions_to}Status: Draft\n{post_history}",
26+
source_path="pep-9000.rst",
27+
parser=Parser(rfc2822=True),
28+
settings_overrides={"docinfo_xform": False},
29+
)
30+
pep_headers.PEPHeaders(document).apply()
31+
return document
32+
33+
34+
@pytest.mark.parametrize(
35+
("last_url", "expected_url", "label"),
36+
[
37+
pytest.param(
38+
"https://discuss.python.org/t/current/200/3",
39+
"https://discuss.python.org/t/current/200",
40+
"Discourse thread",
41+
id="reply-url",
42+
),
43+
pytest.param(
44+
"https://discuss.python.org/t/200/3/",
45+
"https://discuss.python.org/t/200",
46+
"Discourse thread",
47+
id="reply-url-without-a-topic-slug",
48+
),
49+
pytest.param(
50+
"https://discuss.python.org/t/current/200/",
51+
"https://discuss.python.org/t/current/200/",
52+
"Discourse thread",
53+
id="topic-url",
54+
),
55+
pytest.param(
56+
"https://example.com/thread/200/3",
57+
"https://example.com/thread/200/3",
58+
"https://example.com/thread/200/3",
59+
id="other-forum",
60+
),
61+
],
62+
)
63+
def test_discussions_to_uses_last_post_history_link(last_url, expected_url, label):
64+
first_url = "https://discuss.python.org/t/older/100"
65+
document = _render_pep_headers(
66+
f"Post-History: `01-Jan-2026 <{first_url}>`__, "
67+
f"`02-Jan-2026 <{last_url}>`__, 03-Jan-2026\n"
68+
)
69+
70+
fields = {field[0].astext(): field[1] for field in document[0]}
71+
links = list(fields["Discussions-To"].findall(nodes.reference))
72+
assert [(link["refuri"], link.astext()) for link in links] == [
73+
(expected_url, label)
74+
]
75+
assert list(fields).index("Discussions-To") < list(fields).index("Status")
76+
assert [
77+
link["refuri"] for link in fields["Post-History"].findall(nodes.reference)
78+
] == [first_url, last_url]
79+
assert "Discussions-To" not in document["headers"]
80+
81+
82+
@pytest.mark.parametrize(
83+
("value", "label", "expected_urls"),
84+
[
85+
pytest.param(
86+
"https://discuss.python.org/t/explicit/300",
87+
"Discourse thread",
88+
["https://discuss.python.org/t/explicit/300"],
89+
id="explicit-link",
90+
),
91+
pytest.param("Pending", "Pending", [], id="explicit-pending"),
92+
],
93+
)
94+
def test_explicit_discussions_to_takes_precedence(value, label, expected_urls):
95+
document = _render_pep_headers(
96+
"Post-History: `02-Jan-2026 <https://discuss.python.org/t/current/200/3>`__\n",
97+
discussions_to=f"Discussions-To: {value}\n",
98+
)
99+
100+
fields = {field[0].astext(): field[1] for field in document[0]}
101+
body = fields["Discussions-To"]
102+
assert body.astext() == label
103+
assert [link["refuri"] for link in body.findall(nodes.reference)] == expected_urls
104+
assert document["headers"]["Discussions-To"] == value
105+
106+
107+
@pytest.mark.parametrize(
108+
"post_history",
109+
[
110+
pytest.param("Post-History: 02-Jan-2026\n", id="date-without-a-link"),
111+
pytest.param("Post-History: Pending\n", id="pending-history"),
112+
pytest.param("", id="no-history"),
113+
],
114+
)
115+
def test_no_discussions_to_without_post_history_links(post_history):
116+
document = _render_pep_headers(post_history)
117+
118+
assert "Discussions-To" not in {field[0].astext() for field in document[0]}
119+
120+
20121
@pytest.mark.parametrize(
21122
("test_input", "expected"),
22123
[

‎pep_sphinx_extensions/tests/pep_zero_generator/test_pep_index_generator.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import json
2+
3+
import pytest
4+
15
from pep_sphinx_extensions.pep_zero_generator import parser, pep_index_generator
26

37
from ..conftest import PEP_ROOT
@@ -11,6 +15,50 @@ def test_create_pep_json():
1115
assert '"url": "https://peps.python.org/pep-0008/"' in out
1216

1317

18+
@pytest.mark.parametrize(
19+
("headers", "expected"),
20+
[
21+
pytest.param(
22+
"Post-History: `01-Jan-2026 <https://discuss.python.org/t/older/100>`__,\n"
23+
" `02-Jan-2026 <https://discuss.python.org/t/current/200/3>`__,\n"
24+
" 03-Jan-2026\n",
25+
"https://discuss.python.org/t/current/200",
26+
id="last-linked-entry-is-a-reply",
27+
),
28+
pytest.param(
29+
"Post-History: `02-Jan-2026 <https://example.com/thread/200/3>`__\n",
30+
"https://example.com/thread/200/3",
31+
id="other-forum",
32+
),
33+
pytest.param(
34+
"Discussions-To: https://discuss.python.org/t/explicit/300\n"
35+
"Post-History: `02-Jan-2026 <https://discuss.python.org/t/current/200>`__\n",
36+
"https://discuss.python.org/t/explicit/300",
37+
id="explicit-link-takes-precedence",
38+
),
39+
pytest.param(
40+
"Discussions-To: Pending\n"
41+
"Post-History: `02-Jan-2026 <https://discuss.python.org/t/current/200>`__\n",
42+
"Pending",
43+
id="explicit-pending-takes-precedence",
44+
),
45+
pytest.param("Post-History: 02-Jan-2026\n", None, id="date-without-a-link"),
46+
pytest.param("", None, id="no-history"),
47+
],
48+
)
49+
def test_create_pep_json_discussions_to(tmp_path, headers, expected):
50+
path = tmp_path / "pep-9000.rst"
51+
path.write_text(
52+
"PEP: 9000\nTitle: Test PEP\nAuthor: Example Author\n"
53+
f"Status: Draft\nType: Process\nCreated: 01-Jan-2026\n{headers}",
54+
encoding="utf-8",
55+
)
56+
57+
metadata = json.loads(pep_index_generator.create_pep_json([parser.PEP(path)]))
58+
59+
assert metadata["9000"]["discussions_to"] == expected
60+
61+
1462
def test_build_release_peps_links_individual_versions_from_joint_release_pep():
1563
peps = [
1664
parser.PEP(PEP_ROOT / "pep-0361.rst"), # "2.6, 3.0" joint release PEP

0 commit comments

Comments
 (0)