Skip to content

Ignore strings inside subscripts when extracting messages - #1367

Open
Kayvan-Zahiri wants to merge 1 commit into
python-babel:masterfrom
Kayvan-Zahiri:fix/extractor-ignore-subscript-strings-1335
Open

Kayvan-Zahiri wants to merge 1 commit into
python-babel:masterfrom
Kayvan-Zahiri:fix/extractor-ignore-subscript-strings-1335

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

Fixes #1335.

pgettext(context, dictionary["key"]) extracted "key". A dict literal in the same position was extracted too ({"msgid": "value"} became msgidvalue). Those strings are not arguments of the call.

Strings inside [] or {} are skipped. A nested gettext() call is still extracted, and pgettext("navigation", "Guide") is unchanged.

Tests: PYTHONPATH=. python -m pytest --noconftest -o addopts= tests/messages/test_extract.py -q → 12 passed. Before the fix, the new test extracted key and msgidvalue.

Made with Cursor

pgettext(context, dictionary["key"]) treated the subscript key as
the message. Strings inside brackets or braces are not arguments.

Co-authored-by: Cursor <cursoragent@cursor.com>
@hafizahmedzia

Copy link
Copy Markdown

There is an extraction-abort case when an invalid message immediately follows a translator comment:

# Translators: note
_(["ignored"])
_("kept")

With comment_tags=["Translators:"], both extract_python and public extract("python", ...) raise TypeError: '<' not supported between instances of 'int' and 'NoneType' at this PR's recorded head, before yielding kept.

The base completes extraction. At the recorded #1396 head, the public wrapper skips the invalid message and yields (3, 'kept', [], None). Ordinary-comment, no-comment, and tag-disabled controls pass; the PR's original 45 extraction tests also pass, so those controls do not cover this combination.

Credit to denyshon, Kayvan-Zahiri and Cherith1222 for the report and existing work. Codex-assisted recorded checks used Python 3.12.14, base e2a17919f0eb988bfea2883b00793ca4d5d37c4f, this head 653128029d9145363939fd6f33bf8d733a1de92c, and #1396 983e93726bb4fbebfac7f5b35bca7f204f430b07. This was focused extraction coverage, not full CI.

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.

Python extractor treats dict arguments as literal strings

3 participants