From 2ca800944bb8140fd1b1cc7aa877cd71f6904c9b Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Wed, 11 Mar 2026 14:48:11 +0200 Subject: [PATCH] Keep sections in importance order --- .pre-commit-config.yaml | 1 - .ruff.toml | 1 + CHANGELOG.md | 4 ++++ src/blurb/_utils/globs.py | 33 ++++++++++++++++++++++----------- tests/test_utils_globs.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 94fed22..1aab0a4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,6 @@ repos: - id: ruff-check args: [--exit-non-zero-on-fix] - id: ruff-format - args: [--check] - repo: https://github.com/python-jsonschema/check-jsonschema rev: 0.33.2 diff --git a/.ruff.toml b/.ruff.toml index b125bc2..3e19ac2 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -1,4 +1,5 @@ target-version = "py310" +fix = true [format] preview = true diff --git a/CHANGELOG.md b/CHANGELOG.md index 04d4ee9..c76229f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.0 + +* Keep sections in importance order by @hugovk in https://github.com/python/blurb/pull/75 + ## 2.1.0 - Add the `-i` / `--issue` option to the 'blurb add' command. diff --git a/src/blurb/_utils/globs.py b/src/blurb/_utils/globs.py index d4852bd..849d035 100644 --- a/src/blurb/_utils/globs.py +++ b/src/blurb/_utils/globs.py @@ -14,18 +14,29 @@ def glob_blurbs(version: str) -> list[str]: filenames = [] base = os.path.join('Misc', 'NEWS.d', version) + if version != 'next': wildcard = f'{base}.rst' filenames.extend(glob.glob(wildcard)) - else: - sanitized_sections = set(map(sanitize_section, sections)) - sanitized_sections |= set(map(sanitize_section_legacy, sections)) - for section in sanitized_sections: - wildcard = os.path.join(base, section, '*.rst') - entries = glob.glob(wildcard) - deletables = [x for x in entries if x.endswith('/README.rst')] - for filename in deletables: - entries.remove(filename) - filenames.extend(entries) - filenames.sort(reverse=True, key=next_filename_unsanitize_sections) + return filenames + + for section in sections: + entries = [] + seen_dirs = set() + for dir_name in ( + sanitize_section(section), + sanitize_section_legacy(section), + ): + if dir_name in seen_dirs: + continue + + seen_dirs.add(dir_name) + wildcard = os.path.join(base, dir_name, '*.rst') + for entry in glob.glob(wildcard): + if not entry.endswith('/README.rst'): + entries.append(entry) + + entries.sort(reverse=True, key=next_filename_unsanitize_sections) + filenames.extend(entries) + return filenames diff --git a/tests/test_utils_globs.py b/tests/test_utils_globs.py index 97d9cae..aca5f84 100644 --- a/tests/test_utils_globs.py +++ b/tests/test_utils_globs.py @@ -58,3 +58,33 @@ def test_glob_blurbs_sort_order(fs) -> None: # Assert assert filenames == expected + + +def test_glob_blurbs_section_ordering(fs) -> None: + """Sections must appear in importance order, not alphabetical order. + + The canonical order is: Security, Core and Builtins, Library, + Documentation, Tests, Build, Windows, macOS, IDLE, Tools/Demos, C API. + """ + # Arrange: one entry per section + fake_news_entries = [ + 'Misc/NEWS.d/next/Security/2024-01-01-00-00-00.gh-issue-00000.aAAAAA.rst', + 'Misc/NEWS.d/next/Core_and_Builtins/2024-01-01-00-00-00.gh-issue-00001.bBBBBB.rst', + 'Misc/NEWS.d/next/Library/2024-01-01-00-00-00.gh-issue-00002.cCCCCC.rst', + 'Misc/NEWS.d/next/Documentation/2024-01-01-00-00-00.gh-issue-00003.dDDDDD.rst', + 'Misc/NEWS.d/next/Tests/2024-01-01-00-00-00.gh-issue-00004.eEEEEE.rst', + 'Misc/NEWS.d/next/Build/2024-01-01-00-00-00.gh-issue-00005.fFFFFF.rst', + 'Misc/NEWS.d/next/Windows/2024-01-01-00-00-00.gh-issue-00006.gGGGGG.rst', + 'Misc/NEWS.d/next/macOS/2024-01-01-00-00-00.gh-issue-00007.hHHHHH.rst', + 'Misc/NEWS.d/next/IDLE/2024-01-01-00-00-00.gh-issue-00008.iIIIII.rst', + 'Misc/NEWS.d/next/Tools-Demos/2024-01-01-00-00-00.gh-issue-00009.jJJJJJ.rst', + 'Misc/NEWS.d/next/C_API/2024-01-01-00-00-00.gh-issue-00010.kKKKKK.rst', + ] + for path in fake_news_entries: + fs.create_file(path) + + # Act + filenames = glob_blurbs('next') + + # Assert: must be in importance order, not alphabetical + assert filenames == fake_news_entries