Skip to content

Commit

Permalink
Lint: Remove Version, Last-Modified, and Content-Type from `check-pep…
Browse files Browse the repository at this point in the history
…s.py`
  • Loading branch information
AA-Turner committed Feb 1, 2025
1 parent 86fae72 commit 60a64d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
22 changes: 6 additions & 16 deletions check-peps.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@
ALL_HEADERS = (
"PEP",
"Title",
"Version",
"Last-Modified",
"Author",
"Sponsor",
"BDFL-Delegate", "PEP-Delegate",
"Discussions-To",
"Status",
"Type",
"Topic",
"Content-Type",
"Requires",
"Created",
"Python-Version",
Expand Down Expand Up @@ -132,16 +129,18 @@ def check_headers(lines: Sequence[str], /) -> MessageIterator:
yield from _validate_pep_number(next(iter(lines), ""))

found_headers = {}
found_header_lines: list[tuple[str, int]] = []
line_num = 0
for line_num, line in enumerate(lines, start=1):
if line.strip() == "":
headers_end_line_num = line_num
break
if match := HEADER_PATTERN.match(line):
header = match[1]
found_header_lines.append((header, line_num))
if header in ALL_HEADERS:
if header not in found_headers:
found_headers[match[1]] = line_num
found_headers[header] = None
else:
yield line_num, f"Must not have duplicate header: {header} "
else:
Expand All @@ -151,11 +150,11 @@ def check_headers(lines: Sequence[str], /) -> MessageIterator:

yield from _validate_required_headers(found_headers.keys())

shifted_line_nums = list(found_headers.values())[1:]
for i, (header, line_num) in enumerate(found_headers.items()):
shifted_line_nums = [line for _, line in found_header_lines[1:]]
for i, (header, line_num) in enumerate(found_header_lines):
start = line_num - 1
end = headers_end_line_num - 1
if i < len(found_headers) - 1:
if i < len(found_header_lines) - 1:
end = shifted_line_nums[i] - 1
remainder = "\n".join(lines[start:end]).removeprefix(f"{header}:")
if remainder != "":
Expand All @@ -182,8 +181,6 @@ def _validate_header(header: str, line_num: int, content: str) -> MessageIterato
yield from _validate_type(line_num, content)
elif header == "Topic":
yield from _validate_topic(line_num, content)
elif header == "Content-Type":
yield from _validate_content_type(line_num, content)
elif header in {"Requires", "Replaces", "Superseded-By"}:
yield from _validate_pep_references(line_num, content)
elif header == "Created":
Expand Down Expand Up @@ -348,13 +345,6 @@ def _validate_topic(line_num: int, line: str) -> MessageIterator:
yield line_num, "Topic must be sorted lexicographically"


def _validate_content_type(line_num: int, line: str) -> MessageIterator:
"""'Content-Type' must be 'text/x-rst'"""

if line != "text/x-rst":
yield line_num, "Content-Type must be 'text/x-rst'"


def _validate_pep_references(line_num: int, line: str) -> MessageIterator:
"""`Requires`/`Replaces`/`Superseded-By` must be 'NNN' PEP IDs"""

Expand Down
24 changes: 0 additions & 24 deletions pep_sphinx_extensions/tests/pep_lint/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,30 +253,6 @@ def test_validate_topic(line: str, expected_warnings: set):
assert found_warnings == expected_warnings


def test_validate_content_type_valid():
warnings = [
warning for (_, warning) in check_peps._validate_content_type(1, "text/x-rst")
]
assert warnings == [], warnings


@pytest.mark.parametrize(
"line",
[
"text/plain",
"text/markdown",
"text/csv",
"text/rtf",
"text/javascript",
"text/html",
"text/xml",
],
)
def test_validate_content_type_invalid(line: str):
warnings = [warning for (_, warning) in check_peps._validate_content_type(1, line)]
assert warnings == ["Content-Type must be 'text/x-rst'"], warnings


@pytest.mark.parametrize(
"line",
[
Expand Down
17 changes: 7 additions & 10 deletions pep_sphinx_extensions/tests/pep_lint/test_pep_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,30 @@ def test_with_fake_pep():
warnings = list(check_peps.check_peps(PEP_9002, content))
assert warnings == [
(1, "PEP must begin with the 'PEP:' header"),
(6, "Must not have invalid header: Version"),
(9, "Must not have duplicate header: Sponsor "),
(10, "Must not have invalid header: Horse-Guards"),
(15, "Must not have invalid header: Content-Type"),
(1, "Must have required header: PEP"),
(1, "Must have required header: Type"),
(
1,
"Headers must be in PEP 12 order. Correct order: Title, Version, "
"Author, Sponsor, BDFL-Delegate, Discussions-To, Status, Topic, "
"Content-Type, Requires, Created, Python-Version, Post-History, "
"Resolution",
"Headers must be in PEP 12 order. Correct order: Title, Author, "
"Sponsor, BDFL-Delegate, Discussions-To, Status, Topic, Requires, "
"Created, Python-Version, Post-History, Resolution",
),
(4, "Author continuation lines must end with a comma"),
(5, "Author line must not be over-indented"),
(7, "Python-Version major part must be 1, 2, or 3: 4.0"),
(
8,
"Sponsor entries must begin with a valid 'Name': "
r"'Sponsor:\nHorse-Guards: Parade'",
),
(8, "Sponsor entries must begin with a valid 'Name': ''"),
(9, "Sponsor entries must begin with a valid 'Name': ''"),
(11, "Created must be a 'DD-mmm-YYYY' date: '1-Jan-1989'"),
(12, "Delegate entries must begin with a valid 'Name': 'Barry!'"),
(13, "Status must be a valid PEP status"),
(14, "Topic must not contain duplicates"),
(14, "Topic must be properly capitalised (Title Case)"),
(14, "Topic must be for a valid sub-index"),
(14, "Topic must be sorted lexicographically"),
(15, "Content-Type must be 'text/x-rst'"),
(16, "PEP references must be separated by comma-spaces (', ')"),
(17, "Discussions-To must be a valid thread URL or mailing list"),
(18, "Post-History must be a 'DD-mmm-YYYY' date: '2-Feb-2000'"),
Expand Down

0 comments on commit 60a64d5

Please sign in to comment.