Skip to content

Warn about inconsistent plural forms during extraction - #1364

Open
saltnpepper97 wants to merge 1 commit into
python-babel:masterfrom
saltnpepper97:fix/inconsistent-plural-warning
Open

saltnpepper97 wants to merge 1 commit into
python-babel:masterfrom
saltnpepper97:fix/inconsistent-plural-warning

Conversation

@saltnpepper97

@saltnpepper97 saltnpepper97 commented Oct 4, 2026 •

Copy link
Copy Markdown

When plural messages share the same singular text and context but use different plural text, Babel currently keeps the first plural without a warning.

This change checks the existing catalog entry before adding the incoming message and logs a warning with the source location and both plural forms when they conflict. The first plural and merged source locations are preserved.

Tests cover conflicting ngettext and npgettext calls, conflicts across files, identical plural text, separate contexts, and mixed singular/plural calls.

Validation on macOS with Python 3.14.2:

  • New regression cases: 3 failed before the fix; all 8 pass after it.
  • Focused tests: 111 passed.
  • Full test suite: 7,839 passed, 7 skipped, and 2 xfailed.
  • Python compilation completed successfully.
  • Pre-commit checks passed for both changed files.

Fixes #1003.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@saltnpepper97 approving at 33c3e3a. The check sits next to the one catalog.add() call in pybabel extract. The case from #1003 now warns, and the first plural still wins.

Head 33c3e3a, merge base e2a1791, Python 3.12.3, pytest 9.1.1, CLDR 48.2 imported:

$ python -m pytest tests/messages/frontend/test_extract.py -q
23 passed in 0.14s

$ python -m pytest tests/messages/frontend/test_extract.py -q   # frontend.py from e2a1791, your tests kept
FAILED tests/messages/frontend/test_extract.py::test_extraction_plural_conflicts[conflicting-ngettext]
FAILED tests/messages/frontend/test_extract.py::test_extraction_plural_conflicts[conflicting-npgettext]
FAILED tests/messages/frontend/test_extract.py::test_extraction_plural_conflicts[conflicting-across-files]
3 failed, 20 passed in 0.17s

$ python -m pytest tests/messages -q
391 passed, 1 skipped, 1 warning in 1.15s

non-blocking: across files the warning names only the second call site, so the user still has to grep for the first one:

$ pybabel extract -o o2.pot x      # x/one.py:1 ngettext("foo", "foos", 2), x/two.py:3 ngettext("foo", "bars", 2)
x/two.py:3: conflicting plurals for 'foo': 'foos' and 'bars'

existing.locations[0] already holds it. With *existing.locations[0] added to the arguments, the line reads x/two.py:3: conflicting plurals for 'foo': 'foos' (first seen at x/one.py:1) and 'bars'. Your 23 tests still pass with that change.

@akx you merged most of frontend.py recently. Would you take this as it is, or with the first location added to the message?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn for inconsistent uses of (p)ngettext plurals?

2 participants