| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
`_(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.
| Back | FazBrowse Home | New Git URL |
Problem
pybabel extract aborts 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):
File "babel/messages/extract.py", line 619, in extract_python messages.append(''.join(buf)) TypeError: sequence item 0: expected str instance, bytes foundRoot cause: _parse_python_string() is annotated -> str | None, but for a byte string literal the ast.Constant branch returns the raw bytes value, 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: a bytes value now emits a UserWarning naming the literal and returns None, 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 on master (reproduces the exact TypeError from the issue), green with this change. Full suite: 31 failed, 6277 passed with the fix vs 32 failed, 6276 passed on master — 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.