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.
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
.