New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pygettext doesn't work with f-strings #77370
Comments
Tested (on windows) with python 3.6, but I guess it's the same in py3.7: # test.py
def hello(x):
print(_(f'hello {x}')) > py pygettext.py test.py
Traceback (most recent call last):
File "C:\Program Files\Python36\Tools\i18n\pygettext.py", line 623, in <module>
if __name__ == '__main__':
File "C:\Program Files\Python36\Tools\i18n\pygettext.py", line 597, in main
for _token in tokens:
File "C:\Program Files\Python36\Tools\i18n\pygettext.py", line 328, in __call__
## 'tstring:', tstring
File "C:\Program Files\Python36\Tools\i18n\pygettext.py", line 382, in __openseen
elif ttype == tokenize.STRING:
File "C:\Program Files\Python36\Tools\i18n\pygettext.py", line 236, in safe_eval
# unwrap quotes, safely
File "<string>", line 1, in <module>
NameError: name 'x' is not defined |
Is there really a use case for this? I would normally expect place holders to get replaced *after* translation, i.e. in the translated text, not before. |
I agree that pygettext should not and can not support f-strings, but the error should be better. The problem is that an f-string expression is tokenized as literal strings, but eval() can't evaluate it out of context. |
Yes, that's what I maeant - sorry I should have phrased better. Curiously, xgettext handles this thing better (...or, far worse actually): it just parses the f-string as a regular one, producing something like |
If there are no objections I'm going to merge PR 6364 in 24 hours. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: