question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some of the converted strings into fstrings are no longer processed by babel

See original GitHub issue

I’m using babel via a custom function _(string,language) that outputs the translated version.

When running pyupgrade over the source files:

@@ -174,7 +173,7 @@ def irccommands(msgdetail):
                         )
                     else:
                         text = _(
-                            "Error opping users from chat %s: %s" % (chat_id, result),
+                            f"Error opping users from chat {chat_id}: {result}",
                             language=mylang,
                         )
 

It causes babel to segfault as per https://github.com/python-babel/babel/issues/715

I would expect pyupgrade not to touch the code in a way that makes it later fail.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
asottilecommented, Jul 13, 2021

you’re misusing translation – those strings aren’t being translated. if anything babel should produce an error for accidentally using string formatting inside of a translation function

babel will scan those (incorrectly) as "Error opping users from chat %s: %s" – then at runtime it will try and translate "Error opping users from chat whatever1: whatever2" which will not match any of your translated IDs so you will always end up with english text. (we had a linter when I was at yelp which prevented this sort of translation misuse – but I believe it was proprietary / lost to closed source)

1reaction
hugovkcommented, Jul 13, 2021

Sounds like Babel doesn’t support f-strings, which is a Babel bug, not pyupgrade.

But can you use the pyupgrade --keep-percent-format option?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pybabel should raise an error when it encounters f-strings #715
and Babel fails (as in it crashes with an exception) to extract strings from ... Some of the converted strings into fstrings are...
Read more >
Babel Documentation - Read the Docs
While message catalogs allow you to localize any messages in your application, there are a number of strings that are used in many...
Read more >
The Flask Mega-Tutorial Part XIII: I18n and L10n
Flask-Babel is initialized like most other Flask extensions: ... The simplest cases are those where literal strings appear in the source ...
Read more >
In case you didn't know: Python 3.8 f-strings support = for self ...
Also, the format method is compatible with other libraries that process strings, for example when using Babel with Flask it can come handy....
Read more >
Babel Changelog — Babel 2.11.0 documentation
The wheel file is no longer marked as universal, since Babel only supports Python 3. ... in some cases, as some more-or-less internal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found