You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pygettext does not work with new f-strings (same quotes) #113604
pygettext is not a module. It is a script in Tools/i18n.
It fails because it currently does not support new tokens for f-strings: FSTRING_START, FSTRING_MIDDLE and FSTRING_END. Previously the tokenizer produced a single STRING token for f-string. It have to be parsed to the AST and processed recursively.
It seems to me, that it works correct in 3.12 with this example.
It is not related to using same quotes. You get the same result with different quotes.
It complains, because it expects a literal string as argument of _(). You get a similar complain about _(x) and _('a'+'b'). It is not error. Perhaps it should be more clear about this.
It correctly finds "Test" in the nested _("Test"). But it does not find it in 3.11, this is a bug.
It does not find nested calls in some other examples, e.g. _(_('a')), class A(f(_('a'))):, def f(x=_('a')):, etc.
It can find false docstrings, e.g. in def f() -> lambda: '''not docstring''':.
So, there are two issues:
pygettext should be more clean about what is error and what is not error. _() can be used with a non-literal string, and it is in general not error, but it can be programming error if f-string is used unintentionally, so it should emit a warning.
In some corner cases pygettext cannot find nested _() calls or found false docstrings. It is desirable to make it more reliable, although processing class and def declarations can be too complicated. Using AST (pygettext: use an AST parser instead of a tokenizer #104400) can make it simpler, but it can have other drawbacks.
Also, the code for handling f-strings in 3.11 is no longer used and can be deleted.
Activity
terryjreedy commentedon Jan 1, 2024
The print works, at least on Windows 3.13.0a2, but there does not seem to be a pygettext module.
serhiy-storchaka commentedon Jan 2, 2024
pygettextis not a module. It is a script inTools/i18n.It fails because it currently does not support new tokens for f-strings: FSTRING_START, FSTRING_MIDDLE and FSTRING_END. Previously the tokenizer produced a single STRING token for f-string. It have to be parsed to the AST and processed recursively.
serhiy-storchaka commentedon Jan 2, 2024
It seems to me, that it works correct in 3.12 with this example.
_(). You get a similar complain about_(x)and_('a'+'b'). It is not error. Perhaps it should be more clear about this."Test"in the nested_("Test"). But it does not find it in 3.11, this is a bug._(_('a')),class A(f(_('a'))):,def f(x=_('a')):, etc.def f() -> lambda: '''not docstring''':.So, there are two issues:
pygettextshould be more clean about what is error and what is not error._()can be used with a non-literal string, and it is in general not error, but it can be programming error if f-string is used unintentionally, so it should emit a warning.pygettextcannot find nested_()calls or found false docstrings. It is desirable to make it more reliable, although processingclassanddefdeclarations can be too complicated. Using AST (pygettext: use an AST parser instead of a tokenizer #104400) can make it simpler, but it can have other drawbacks.Also, the code for handling f-strings in 3.11 is no longer used and can be deleted.
tomasr8 commentedon Feb 16, 2025
Fixed in #104402