|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.12" |
| 3 | +# dependencies = ["trove-classifiers>=2026.6.1.19"] |
| 4 | +# /// |
| 5 | +""" |
| 6 | +The Python releases the formatter's defaults stand for. |
| 7 | +
|
| 8 | +The two ends answer different questions and are read from different places. The newest release a |
| 9 | +project may be given a classifier for has to satisfy two things at once: PyPI publishes a classifier |
| 10 | +naming it, and it has reached its release candidate. Either alone says too little, since a |
| 11 | +classifier appears while a release is still changing and a candidate PyPI has no classifier for |
| 12 | +cannot be written down. The oldest is the one still carrying fixes, which a classifier outlives by |
| 13 | +years and so cannot say. |
| 14 | +
|
| 15 | +Nothing here is predicted. Each run reads what is true that day and writes down the numbers, so a |
| 16 | +release that slips arrives when it arrives. Someone naming `max_supported_python` themselves is |
| 17 | +saying what their own project supports, which no rule here overrides. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import json |
| 23 | +import re |
| 24 | +import sys |
| 25 | +import urllib.request |
| 26 | +from dataclasses import dataclass |
| 27 | +from datetime import UTC, datetime |
| 28 | +from pathlib import Path |
| 29 | +from typing import Final |
| 30 | + |
| 31 | +from trove_classifiers import classifiers |
| 32 | + |
| 33 | +_ROOT: Final[Path] = Path(__file__).resolve().parents[1] |
| 34 | +_MAJOR: Final[int] = 3 |
| 35 | + |
| 36 | + |
| 37 | +def main(*, check: bool) -> None: |
| 38 | + places = written_places(newest_candidate(), oldest_supported()) |
| 39 | + drifted = [(place, said) for place in places if (said := place.said()) != place.spelling] |
| 40 | + if not drifted: |
| 41 | + print(f"the defaults name {_MAJOR}.{places[-1].minor} through {_MAJOR}.{places[0].minor}") |
| 42 | + return |
| 43 | + for place, said in drifted: |
| 44 | + print(f"{place.path.relative_to(_ROOT)}: {said} -> {place.spelling}") |
| 45 | + if check: |
| 46 | + sys.exit(1) |
| 47 | + for place, said in drifted: |
| 48 | + place.write(said) |
| 49 | + |
| 50 | + |
| 51 | +def newest_candidate() -> int: |
| 52 | + """ |
| 53 | + The newest release both PyPI and Python itself have got to: a classifier naming it, and a |
| 54 | + release candidate published. |
| 55 | +
|
| 56 | + A release still in alpha or beta is one whose own behavior is still moving, so a project saying |
| 57 | + it runs there says more than it knows. A candidate PyPI publishes no classifier for is one no |
| 58 | + project can name, since an upload carrying an unknown classifier is turned away. |
| 59 | + """ |
| 60 | + return max(with_a_candidate() & with_a_classifier()) |
| 61 | + |
| 62 | + |
| 63 | +def with_a_candidate() -> set[int]: |
| 64 | + """The releases Python has published a candidate for.""" |
| 65 | + released = fetch("https://www.python.org/api/v2/downloads/release/?is_published=true") |
| 66 | + named = re.compile(rf"^Python {_MAJOR}\.(\d+)\.\d+rc\d+$") |
| 67 | + return {int(found[1]) for release in released if (found := named.match(release["name"]))} |
| 68 | + |
| 69 | + |
| 70 | +def with_a_classifier() -> set[int]: |
| 71 | + """The releases PyPI publishes a classifier for.""" |
| 72 | + prefix = f"Programming Language :: Python :: {_MAJOR}." |
| 73 | + return { |
| 74 | + int(minor) for name in classifiers if name.startswith(prefix) and (minor := name.removeprefix(prefix)).isdigit() |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +def oldest_supported() -> int: |
| 79 | + """The oldest release still carrying fixes, which is the oldest the formatter writes for.""" |
| 80 | + today = datetime.now(tz=UTC).date().isoformat() |
| 81 | + cycles = fetch("https://endoflife.date/api/python.json") |
| 82 | + live = (row for row in cycles if isinstance(row["eol"], str) and row["eol"] > today) |
| 83 | + return min(int(row["cycle"].split(".")[1]) for row in live) |
| 84 | + |
| 85 | + |
| 86 | +def fetch(url: str) -> list[dict[str, str]]: |
| 87 | + with urllib.request.urlopen(url, timeout=30) as response: # ruff: ignore[suspicious-url-open-usage] # a named host |
| 88 | + return json.load(response) |
| 89 | + |
| 90 | + |
| 91 | +def written_places(newest: int, oldest: int) -> list[Place]: |
| 92 | + """Every file that spells either release, the newest ones first.""" |
| 93 | + spelled = [ |
| 94 | + ("pyproject-fmt/src/pyproject_fmt/__main__.py", r"default=\(\d+, \d+\)", f"default=({_MAJOR}, {newest})"), |
| 95 | + ( |
| 96 | + "tasks/fmt_examples.py", |
| 97 | + r'"max_supported_python": \(\d+, \d+\)', |
| 98 | + f'"max_supported_python": ({_MAJOR}, {newest})', |
| 99 | + ), |
| 100 | + ( |
| 101 | + "pyproject-fmt/pyproject.toml", |
| 102 | + r'max_supported_python = "\d+\.\d+"', |
| 103 | + f'max_supported_python = "{_MAJOR}.{newest}"', |
| 104 | + ), |
| 105 | + ( |
| 106 | + "tox-toml-fmt/pyproject.toml", |
| 107 | + r'max_supported_python = "\d+\.\d+"', |
| 108 | + f'max_supported_python = "{_MAJOR}.{newest}"', |
| 109 | + ), |
| 110 | + ( |
| 111 | + "pyproject-fmt/docs/configuration.rst", |
| 112 | + r'max_supported_python = "\d+\.\d+"', |
| 113 | + f'max_supported_python = "{_MAJOR}.{newest}"', |
| 114 | + ), |
| 115 | + ( |
| 116 | + "pyproject-fmt/docs/formatting.rst", |
| 117 | + r"``max_supported_python`` \(here ``\d+\.\d+``\)", |
| 118 | + f"``max_supported_python`` (here ``{_MAJOR}.{newest}``)", |
| 119 | + ), |
| 120 | + ] |
| 121 | + places = [Place(_ROOT / name, pattern, newest, spelling) for name, pattern, spelling in spelled] |
| 122 | + places.append( |
| 123 | + Place( |
| 124 | + _ROOT / "pyproject-fmt/src/pyproject_fmt/__main__.py", |
| 125 | + r"_MIN_SUPPORTED_PYTHON: Final\[tuple\[int, int\]\] = \(_PYTHON_MAJOR, \d+\)", |
| 126 | + oldest, |
| 127 | + f"_MIN_SUPPORTED_PYTHON: Final[tuple[int, int]] = (_PYTHON_MAJOR, {oldest})", |
| 128 | + ) |
| 129 | + ) |
| 130 | + return places |
| 131 | + |
| 132 | + |
| 133 | +@dataclass(frozen=True) |
| 134 | +class Place: |
| 135 | + """One spelling of a release, in the file that writes it.""" |
| 136 | + |
| 137 | + path: Path |
| 138 | + pattern: str |
| 139 | + minor: int |
| 140 | + spelling: str |
| 141 | + |
| 142 | + def said(self) -> str: |
| 143 | + """What the file writes there, which a run holds against the spelling it should write.""" |
| 144 | + found = re.search(self.pattern, self.path.read_text(encoding="utf-8")) |
| 145 | + if found is None: |
| 146 | + print(f"{self.path.relative_to(_ROOT)}: nothing matches {self.pattern}") |
| 147 | + sys.exit(1) |
| 148 | + return found.group(0) |
| 149 | + |
| 150 | + def write(self, said: str) -> None: |
| 151 | + self.path.write_text(self.path.read_text(encoding="utf-8").replace(said, self.spelling), encoding="utf-8") |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main(check="--check" in sys.argv[1:]) |
0 commit comments