fix(extract): skip bytes literals in _parse_python_string (#1190) - #1298
Open
Mukller wants to merge 2 commits into
Open
fix(extract): skip bytes literals in _parse_python_string (#1190)#1298Mukller wants to merge 2 commits into
Mukller wants to merge 2 commits into
Conversation
…el#1190) _parse_python_string() is typed -> str | None but was returning bytes for literals like b'foo'. The caller appended the bytes value to `buf` and the subsequent ''.join(buf) raised: TypeError: sequence item 0: expected str instance, bytes found Guard the return with isinstance(body.value, str) so that byte-string literals are treated the same as other non-extractable expressions (i.e. silently ignored). bytes cannot be used as gettext messages anyway.
Mukller
commented
Jul 29, 2026
Mukller
left a comment
Author
There was a problem hiding this comment.
Code Review
Bug Trace
# source.py: _(b'foo')
# tokenize produces: tok=STRING, value="b'foo'"
# _parse_python_string("b'foo'", encoding='UTF-8', future_flags=0)
# compile("b'foo'", '<string>', 'eval', ...) -> ast.Expression
# body = ast.Constant(value=b'foo') # bytes!
# isinstance(body, ast.Constant) -> True
# return body.value # returns b'foo' (bytes)
# Back in extract_python:
# val = b'foo' (not None, so not skipped)
# buf.append(b'foo') # buf = [b'foo']
# ''.join(buf) # TypeError: expected str, got bytesFix Correctness
# After fix:
if isinstance(body, ast.Constant):
if isinstance(body.value, str): # new guard
return body.value
# bytes falls through to implicit 'return None'| Token | body.value type |
Before | After |
|---|---|---|---|
'foo' |
str |
Returns 'foo' ✓ |
Returns 'foo' ✓ |
b'foo' |
bytes |
Returns b'foo' → crash ✗ |
Returns None → skipped ✓ |
42 |
int |
Returns 42 → crash ✗ |
Returns None → skipped ✓ |
3.14 |
float |
Returns 3.14 → crash ✗ |
Returns None → skipped ✓ |
Note: integer and float ast.Constant values would cause the same crash — this guard also fixes those edge cases.
Scope
- 2 lines changed inside
_parse_python_string(). - No new imports needed.
- Extracted
strmessages are unaffected. - The return type annotation
-> str | Noneis now correct.
akx
reviewed
Jul 30, 2026
Comment on lines
+713
to
+714
| if isinstance(body.value, str): | ||
| return body.value |
Member
There was a problem hiding this comment.
As noted in #1190, it would be useful to warn about an invalid value, instead of quietly ignoring it.
Author
There was a problem hiding this comment.
Done — added a SyntaxWarning in the latest commit. The warning includes the literal value and a note to use a str literal instead:
SyntaxWarning: Bytes literal b'foo' passed to a gettext function; it will be skipped during message extraction. Use a str literal instead.
Instead of silently returning None, emit a SyntaxWarning so users know their _(b"...") call is being skipped during extraction. Addresses review feedback from @akx on PR python-babel#1298.
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.
Summary
Fixes #1190.
Running
pybabel extracton a file containing_(b'foo')(a byte-string literal as a translation argument) crashes with:Root Cause
_parse_python_string()is annotated as-> str | Nonebut returnsbytesfor byte-string literals. The function compiles the token to anast.Constantand returnsbody.valueunconditionally — forb'foo'that isb'foo'(bytes):The caller in
extract_pythonappends the returned value to a string fragment listbuf, then joins it:Fix
Guard the return with
isinstance(body.value, str)so that byte-string literals returnNoneand are silently skipped, exactly like any other non-extractable expression:Bytes literals are not valid
gettextmessages, so ignoring them (rather than crashing) is the correct and expected behavior.Test