Skip to content

Add a --check mode to the extract command - #1444

Open
chrikrah wants to merge 2 commits into
python-babel:masterfrom
chrikrah:extract-check-900
Open

chrikrah wants to merge 2 commits into
python-babel:masterfrom
chrikrah:extract-check-900

Conversation

@chrikrah

@chrikrah chrikrah commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #900.

pybabel extract --check writes nothing and fails when the POT file would change, ignoring POT-Creation-Date.

$ pybabel extract -o messages.pot app && pybabel extract --check -o messages.pot app
PO template file messages.pot is up to date.                         # exit 0
$ echo 'print(_("Bye"))' >> app/a.py && pybabel extract --check -o messages.pot app
babel.messages.frontend.BaseError: PO template file messages.pot is out of date.   # exit 1

$ python -X utf8 -m pytest -q                       # at a18709c, CLDR imported
8210 passed, 7 skipped, 2 xfailed                    # base
8212 passed, 7 skipped, 2 xfailed                    # this branch
2 failed, 21 passed                                  # tests/messages/frontend/test_cli.py with frontend.py from a18709c
$ pre-commit run --all-files                         # ruff 0.14.10 and the rest: Passed

A missing file fails the check, the way update --check --init-missing treats a catalog it would create (frontend.py#L809-L812). In CI that catches a POT nobody committed yet. Should a missing file pass instead?

Like `update --check`, it reports whether the output file is up to date
without writing it. POT-Creation-Date is ignored, and a missing output
file counts as out of date, as with `update --check --init-missing`.

Fixes python-babel#900

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.16%. Comparing base (a18709c) to head (701dc27).
⚠️ Report is 37 commits behind head on master.

Files with missing lines Patch % Lines
babel/messages/frontend.py 88.23% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1444      +/-   ##
==========================================
+ Coverage   93.62%   94.16%   +0.54%     
==========================================
  Files          27       27              
  Lines        4973     5091     +118     
==========================================
+ Hits         4656     4794     +138     
+ Misses        317      297      -20     
Flag Coverage Δ
macos-15-3.13 ?
macos-15-3.15 ?
macos-26-3.10 93.26% <88.23%> (?)
macos-26-3.11 93.20% <88.23%> (?)
macos-26-3.12 93.45% <88.23%> (?)
macos-26-3.13 93.45% <88.23%> (?)
macos-26-3.14 93.43% <88.23%> (?)
macos-26-3.15 93.43% <88.23%> (?)
macos-26-3.9 93.19% <88.23%> (?)
macos-26-pypy3.11 93.20% <88.23%> (?)
macos-26-pypy3.12 93.45% <88.23%> (?)
ubuntu-24.04-3.10 93.28% <88.23%> (+0.56%) ⬆️
ubuntu-24.04-3.11 93.22% <88.23%> (+0.56%) ⬆️
ubuntu-24.04-3.12 93.47% <88.23%> (?)
ubuntu-24.04-3.13 93.47% <88.23%> (+0.55%) ⬆️
ubuntu-24.04-3.14 93.45% <88.23%> (+0.55%) ⬆️
ubuntu-24.04-3.15 93.45% <88.23%> (+0.55%) ⬆️
ubuntu-24.04-3.9 93.21% <88.23%> (+0.56%) ⬆️
ubuntu-24.04-pypy3.10 ?
ubuntu-24.04-pypy3.11 93.22% <88.23%> (?)
ubuntu-24.04-pypy3.12 93.47% <88.23%> (?)
windows-2022-3.10 ?
windows-2022-3.15 ?
windows-2022-3.9 ?
windows-2025-3.10 93.27% <88.23%> (?)
windows-2025-3.11 93.21% <88.23%> (?)
windows-2025-3.12 93.47% <88.23%> (?)
windows-2025-3.13 93.47% <88.23%> (?)
windows-2025-3.14 93.45% <88.23%> (?)
windows-2025-3.15 93.45% <88.23%> (?)
windows-2025-3.9 93.21% <88.23%> (?)
windows-2025-pypy3.11 93.21% <88.23%> (?)
windows-2025-pypy3.12 93.47% <88.23%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing chrikrah:extract-check-900 (701dc27) with master (b778b94)

Open in CodSpeed

@manav4u

manav4u commented Nov 26, 2026

Copy link
Copy Markdown

One false-negative case on 1fcd0ea: changing only --copyright-holder still passes --check, although a normal extraction changes the POT copyright line.

printf '_("Hello")\n' > app.py
pybabel extract --copyright-holder 'Old Organization' -o messages.pot app.py
pybabel extract --check --copyright-holder 'New Organization' -o messages.pot app.py
# exits 0: "is up to date"
pybabel extract --copyright-holder 'New Organization' -o messages.pot app.py
# changes "Copyright (C) 2026 Old Organization" to "... New Organization"

Catalog.is_identical() compares messages and MIME headers, but not header_comment, where the copyright holder is rendered. Could the check compare that comment too? This also matters for a changed custom header with the same line count.

Reproduced on Linux CPython 3.10 and 3.14. The unchanged-option control passes, and check mode preserves the file's bytes and mtime. The existing CLI tests pass on 3.14 (23 passed). I used AI assistance for this review; no downstream application impact claimed.

`Catalog.is_identical()` leaves out the header comment, so a changed
`--copyright-holder` or `--header-comment` passed the check although a
normal extraction rewrites those lines.

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@manav4u Reproduced at 1fcd0ea, and the same-line-count --header-comment case too. Fixed in 701dc27: the check now also compares header_comment.

$ python -X utf8 -m pytest -q        # 1fcd0ea, CLDR imported, .[dev]
8212 passed, 7 skipped, 2 xfailed
$ python -X utf8 -m pytest -q        # 701dc27
8214 passed, 7 skipped, 2 xfailed
$ git checkout 1fcd0ea -- babel/messages/frontend.py && python -X utf8 -m pytest -q
2 failed, 8212 passed, 7 skipped, 2 xfailed    # both test_extract_check_header_comment cases
$ pre-commit run --all-files         # ruff v0.14.10
Passed

One cost: the header carries the current year, so last year's template fails --check once in January. I left it in, since a real extraction rewrites that line and the file really is stale. Should the year be masked the way POT-Creation-Date is, or is the January failure fine for your CI?

@manav4u

manav4u commented Nov 26, 2026

Copy link
Copy Markdown

Confirmed on 701dc27: both the copyright-holder change and the same-line-count custom header now fail --check; unchanged-option controls pass, and check mode leaves the file's bytes and mtime untouched. The 25 CLI tests pass on Python 3.10 and 3.14, Linux.

I don't have a downstream CI deployment to speak for. My preference would be to keep the year comparison as you have it: this option is documented as detecting what extraction would change, with POT-Creation-Date as the explicit exception. Masking the year would add another exception and could also hide a deliberately edited custom header. I'd leave that policy call to the maintainers rather than block this repair on it.

AI-assisted local verification; no full-suite or hosted-CI claim.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check mode for exctract command

3 participants