Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions pep_sphinx_extensions/pep_processor/transforms/pep_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ def apply(self) -> None:
if len(header) < 2 or header[1][0].astext().lower() != "title":
raise PEPParsingError("No title!")

generated_discussions_to = _insert_discussions_to_fallback(header)
fields_to_remove = []
self.document["headers"] = headers = {}
for field in header:
row_attributes = {sub.tagname: sub.rawsource for sub in field}
headers[row_attributes["field_name"]] = row_attributes["field_body"]
# Only source fields belong in the raw header metadata.
if field is not generated_discussions_to:
row_attributes = {sub.tagname: sub.rawsource for sub in field}
headers[row_attributes["field_name"]] = row_attributes["field_body"]

name = field[0].astext().lower()
body = field[1]
Expand Down Expand Up @@ -199,6 +202,43 @@ def apply(self) -> None:
field.parent.remove(field)


def _insert_discussions_to_fallback(header: nodes.field_list) -> nodes.field | None:
post_history = None
insert_at = len(header)
for index, field in enumerate(header):
name = field[0].astext().lower()
if name == "discussions-to":
return None
if name == "post-history":
post_history = field[1]
elif name == "status":
insert_at = index

if post_history is None:
return None

for reference in reversed(list(post_history.findall(nodes.reference))):
if url := reference.get("refuri"):
break
else:
return None
Comment on lines +220 to +224

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might be a bit clearer?

Suggested change
for reference in reversed(list(post_history.findall(nodes.reference))):
if url := reference.get("refuri"):
break
else:
return None
urls = [
reference["refuri"]
for reference in post_history.findall(nodes.reference)
if reference.get("refuri")
]
if not urls:
return None
url = urls[-1]


# A Discourse post URL ends in a reply number after the topic ID.
url = re.sub(
r"^(https://discuss\.python\.org/t/(?:[\w-]+/)?[0-9]+)/[0-9]+/?$",
r"\1",
url,
)
link = nodes.reference("", url, refuri=url)
discussion_field = nodes.field(
"",
nodes.field_name("", "Discussions-To"),
nodes.field_body("", nodes.paragraph("", "", link)),
)
header.insert(insert_at, discussion_field)
return discussion_field


def _generate_list_url(mailto: str) -> str:
list_name_domain = mailto.lower().removeprefix("mailto:").strip()
list_name = list_name_domain.split("@")[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest
from docutils import nodes
from docutils.core import publish_doctree
from docutils.parsers.rst import Parser

from pep_sphinx_extensions.pep_processor.transforms import pep_headers
from pep_sphinx_extensions.pep_zero_generator.constants import (
Expand All @@ -17,6 +20,89 @@
)


@pytest.mark.parametrize(
("discussions_to", "post_history", "expected"),
[
pytest.param(
"",
"`1-Jan-2026 <https://discuss.python.org/t/older/100>`__, "
"`2-Jan-2026 <https://discuss.python.org/t/current/200/3>`__, 3-Jan-2026",
"https://discuss.python.org/t/current/200",
id="last-link-points-to-a-reply",
),
pytest.param(
"",
"`2-Jan-2026 <https://discuss.python.org/t/200/3/>`__",
"https://discuss.python.org/t/200",
id="reply-url-without-a-topic-slug",
),
pytest.param(
"",
"`2-Jan-2026 <https://discuss.python.org/t/current/200/>`__",
"https://discuss.python.org/t/current/200/",
id="topic-url",
),
pytest.param(
"",
"`2-Jan-2026 <https://example.com/thread/200/3>`__",
"https://example.com/thread/200/3",
id="other-forum",
),
pytest.param(
"Discussions-To: https://discuss.python.org/t/explicit/300\n",
"`2-Jan-2026 <https://discuss.python.org/t/current/200/3>`__",
"https://discuss.python.org/t/explicit/300",
id="explicit-link-takes-precedence",
),
pytest.param(
"Discussions-To: Pending\n",
"`2-Jan-2026 <https://discuss.python.org/t/current/200/3>`__",
"Pending",
id="explicit-pending-takes-precedence",
),
pytest.param("", "2-Jan-2026", None, id="date-without-a-link"),
pytest.param("", "Pending", None, id="pending-history"),
pytest.param("", None, None, id="no-history"),
],
)
def test_discussions_to_fallback(discussions_to, post_history, expected):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's quite a bit of logic in this test.

Can you split it out into their own test cases with the relevant pytest.params?

source = f"PEP: 9000\nTitle: Test PEP\n{discussions_to}Status: Draft\n"
if post_history is not None:
source += f"Post-History: {post_history}\n"
document = publish_doctree(
source,
source_path="pep-9000.rst",
parser=Parser(rfc2822=True),
settings_overrides={"docinfo_xform": False},
)
header = document[0]
source_fields = {field[0].astext(): field[1] for field in header}
if post_history is not None:
history_link_urls = [
link["refuri"]
for link in source_fields["Post-History"].findall(nodes.reference)
]

pep_headers.PEPHeaders(document).apply()

fields = {field[0].astext(): field[1] for field in header}
if expected is None:
assert "Discussions-To" not in fields
elif expected == "Pending":
assert fields["Discussions-To"].astext() == expected
else:
links = list(fields["Discussions-To"].findall(nodes.reference))
assert [link["refuri"] for link in links] == [expected]
if expected.startswith("https://discuss.python.org/"):
assert links[0].astext() == "Discourse thread"
assert list(fields).index("Discussions-To") < list(fields).index("Status")
if post_history is not None:
assert [
link["refuri"] for link in fields["Post-History"].findall(nodes.reference)
] == history_link_urls
assert ("Discussions-To" in document["headers"]) == bool(discussions_to)


@pytest.mark.parametrize(
("test_input", "expected"),
[
Expand Down
Loading