vscode python gettext : Pylance reports undefined variable '_'
Question:
In a localized program I use gettext.install() in the main modul src/testgettext.py.
The documentation (python 3.10.7) says
gettext.install(domain, localedir=None, codeset=None, names=None)
This installs the function _() in Python’s builtins namespace…
The code
import gettext
gettext.install("any")
de = gettext.translation('testgettext',localedir="src/locales", languages=['de'])
de.install()
print(_("Selected Files:"))
functions as awaited, producing ‘Ausgewählte Dateien:’ from ‘src/locales/de/LC_MESSAGES/testgettext.po’.
Nevertheless I get the warning
"_" is not defined Pylance(reportUndefinedVariable) …
In my original main file this "problem" occures 85 times, cluttering any important problems.
Inserting
_ = t.gettext
removes the pylance-problem-message, but only on module level.
python-gettext-docu:
This puts _() only in the module’s global namespace and so only affects calls within this module.
All other modules would have to be altered, too.
So my question:
what to do in order to use python gettext in vcode without unnecessary pylance warnings about ‘_()’?
BTW: in eclipse/pydev the code functions without any problems.
Answers:
There is nothing wrong with pylance’s report that it cannot detect that _
is defined by gettext.install()
and therefore assumes it is an undefined variable.
You can indicate that this _
is a function that returns a translated string by adding a type hint
from typing import Callable
def _(s: str) -> str:
return s
Finally I found the answer:
…you may want to tell Pyright about these additional symbols that are available at runtime. To do so, you can add a local type stub file called __builtins__.pyi
Inserting a file called __builtins__.pyi
with the contents _ = str
in the base of the project will be interpreted by pyright so that _
is evaluated as a global symbol of type str
.
In a localized program I use gettext.install() in the main modul src/testgettext.py.
The documentation (python 3.10.7) says
gettext.install(domain, localedir=None, codeset=None, names=None)
This installs the function _() in Python’s builtins namespace…
The code
import gettext
gettext.install("any")
de = gettext.translation('testgettext',localedir="src/locales", languages=['de'])
de.install()
print(_("Selected Files:"))
functions as awaited, producing ‘Ausgewählte Dateien:’ from ‘src/locales/de/LC_MESSAGES/testgettext.po’.
Nevertheless I get the warning
"_" is not defined Pylance(reportUndefinedVariable) …
In my original main file this "problem" occures 85 times, cluttering any important problems.
Inserting
_ = t.gettext
removes the pylance-problem-message, but only on module level.
python-gettext-docu:
This puts _() only in the module’s global namespace and so only affects calls within this module.
All other modules would have to be altered, too.
So my question:
what to do in order to use python gettext in vcode without unnecessary pylance warnings about ‘_()’?
BTW: in eclipse/pydev the code functions without any problems.
There is nothing wrong with pylance’s report that it cannot detect that _
is defined by gettext.install()
and therefore assumes it is an undefined variable.
You can indicate that this _
is a function that returns a translated string by adding a type hint
from typing import Callable
def _(s: str) -> str:
return s
Finally I found the answer:
…you may want to tell Pyright about these additional symbols that are available at runtime. To do so, you can add a local type stub file called
__builtins__.pyi
Inserting a file called __builtins__.pyi
with the contents _ = str
in the base of the project will be interpreted by pyright so that _
is evaluated as a global symbol of type str
.