Conversation
`_(b'foo')` crashed `pybabel extract` with `TypeError: sequence item 0: expected str instance, bytes found` because `_parse_python_string` returned the raw `bytes` value of an `ast.Constant`, violating its `str | None` annotation, and the value was later fed to `''.join(buf)`. Byte string literals are not translatable messages, so following the behavior agreed in python-babel#1190 they are now skipped with a UserWarning instead of aborting the whole extraction run. Fixes python-babel#1190.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
pybabel extractaborts the whole run when a keyword call receives a byte string literal, e.g. a source file containing_(b'foo')(as produced by some tools converting legacy code):Root cause:
_parse_python_string()is annotated-> str | None, but for a byte string literal theast.Constantbranch returns the rawbytesvalue, which later reaches''.join(buf).As agreed in #1190, byte string literals are not translatable messages, so they should be skipped with a warning rather than crashing the extraction.
Fix
In
_parse_python_string(), check the constant's type: abytesvalue now emits aUserWarningnaming the literal and returnsNone, which the extractor already handles as "nothing extracted" (the same way non-constant arguments such as_(hello.there)are skipped).Tests
test_extract_byte_string_literal_warns_and_skips— red onmaster(reproduces the exactTypeErrorfrom the issue), green with this change. Full suite:31 failed, 6277 passedwith the fix vs32 failed, 6276 passedonmaster— the only delta is the new test; the remaining failures are the pre-existing CLDR-data-drift cases (this checkout runs against release locale data).Fixes #1190.
Disclosure: this pull request was prepared with the assistance of an AI coding agent (analysis, patch and tests), and was reviewed and submitted by a human contributor.