Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,16 @@ def _parse_python_string(value: str, encoding: str, future_flags: int) -> str |
if isinstance(code, ast.Expression):
body = code.body
if isinstance(body, ast.Constant):
return body.value
if isinstance(body.value, str):
return body.value
Comment on lines +713 to +714

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As noted in #1190, it would be useful to warn about an invalid value, instead of quietly ignoring it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

if isinstance(body.value, bytes):
warnings.warn(
f"Bytes literal {value!r} passed to a gettext function; "
"it will be skipped during message extraction. "
"Use a str literal instead.",
SyntaxWarning,
stacklevel=2,
)
if isinstance(body, ast.JoinedStr): # f-string
if all(isinstance(node, ast.Constant) for node in body.values):
return ''.join(node.value for node in body.values)
Expand Down