Skip to content

check-peps: validate every Python-Version entry, not just the first - #5225

Closed
Dhairya-007 wants to merge 2 commits into
python:mainfrom
Dhairya-007:fix/python-version-validation-loop
Closed

Dhairya-007 wants to merge 2 commits into
python:mainfrom
Dhairya-007:fix/python-version-validation-loop

Conversation

@Dhairya-007

Copy link
Copy Markdown
Contributor

_validate_python_version loops over the comma-separated versions in a
Python-Version header, but the "no micro part" branch used return instead of
continue:

for version in versions:
    ...
    if micro == "":
        return          # ends the generator, not just this iteration
    if minor == "x":
        yield ...

The function is a generator, so return ends the whole validation rather than
moving on to the next version. Two-segment versions (3.12, 3.0) are the
overwhelming majority, so in practice only the first entry of a
Python-Version header was ever checked:

>>> _validate_python_version(1, "3.0, 3.a")       # minor part is not numeric
[]
>>> _validate_python_version(1, "3.12, 4.0")      # major part is not 1, 2 or 3
[]
>>> _validate_python_version(1, "3.11, 3.x.1")    # micro part with an 'x' minor
[]
>>> _validate_python_version(1, "3.9, 3.09")      # leading zero in the minor
[]

All four are reported after the change. None of the checks were missing — they
were simply unreachable for every version after the first.

return → continue is the whole fix.

Checks

  • python3 check-peps.py over all PEPs still exits 0, so no currently committed
    PEP trips the newly reachable checks — this does not turn CI red.
  • The existing test_validate_python_version builds its expected messages from
    the whole line, so it can only describe single-version headers. I added
    test_validate_python_version_all_versions for lists; five of its seven cases
    fail without the fix.
  • pytest pep_sphinx_extensions/tests/pep_lint/ → 469 passed, and black and
    ruff are clean on both files.

One deliberate non-change

An AST sweep for return inside a loop in a generator turns up one other hit,
_validate_discussions_to. There the return is correct — it suppresses the
catch-all "must be a valid thread URL, mailing list, or 'Pending'" error once a
known suffix matches — so I left it alone.

…sion

_validate_python_version loops over the comma-separated versions in the header,
but the "no micro part" branch used return rather than continue. Because it is
a generator, that ended the whole validation instead of moving to the next
version, so everything after the first X.Y version went unchecked:

    _validate_python_version(1, "3.0, 3.a")     -> no warnings
    _validate_python_version(1, "3.12, 4.0")    -> no warnings
    _validate_python_version(1, "3.11, 3.x.1")  -> no warnings
    _validate_python_version(1, "3.9, 3.09")    -> no warnings

Two-segment versions are the overwhelming majority, so in practice only the
first entry of a Python-Version header was ever validated. The checks all still
existed; they were simply unreachable.

An AST sweep for return inside a loop in a generator turns up one other hit,
_validate_discussions_to, where the return is deliberate: it suppresses the
catch-all error after a matching suffix. That one is left alone.
Five of these seven cases fail without the previous commit, because validation
stopped at the first version without a micro part. The existing
test_validate_python_version builds its expected messages from the whole line,
so it can only describe single-version headers; this covers lists.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 pep-previews | 🛠️ Build #35738717 | 📁 Comparing 60ebe78 against latest (dfc3d95)

  🔍 Preview build  

7 files changed · ± 7 modified

± Modified

@Dhairya-007

Copy link
Copy Markdown
Contributor Author

@StanFromIreland would you mind taking a look? This one is a check-peps.py logic fix rather than prose: return inside the version loop meant only the first entry of a Python-Version header was ever validated.

@StanFromIreland

Copy link
Copy Markdown
Member

Duplicate of #5221.

@Dhairya-007
Dhairya-007 deleted the fix/python-version-validation-loop branch November 8, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants