1

I do use GNU gettext class-based API. In practice this means that the function _() is not explicit declare by myself in each module like this:

import gettext
t = gettext.translation('spam', '/usr/share/locale')
_ = t.gettext

But it is done like this

import gettext
gettext.install('myapplication')

In the back this install() declares the _() in the back and place it into the builtins namespace so I can use it in every module.

But this is a problem from the point of view of linters like pyflake:

pyflakes: '_' may be undefined, or defined from star imports

Of course I do use _() everywhere in my code but it is not declare elsewhere but implicit via gettext.install().

Any idea how to solve this? There are a lot of locations where I do use _(). So I don't want to put an # noqa behind it.

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • Bad design on the part of `gettext` IMO, in multiple respects. You could maybe wrap it in something that defines a default value for `_` at import time (like a function that just raises an exception). – Samwise Jul 14 '23 at 15:46
  • Some hints and maybe a solution: https://stackoverflow.com/questions/37840142/how-to-avoid-flake8s-f821-undefined-name-when-has-been-installed-by-get – buhtz Jul 14 '23 at 16:05

0 Answers0