Recognize _("text") from gettext.install(...)

Bug #844592 reported by Dima Tisnek
18
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Wishlist
Unassigned

Bug Description

gettext module provides convenience function gettext.install() that injects a callable _ [single underscore] into builtins, so that internationalized code can be written like this:

print _("text")

pyflakes reports every _ as undefined symbol.

moreover since it is injected in builtins, all imported modules have that symbol, thus the following works:

# main.py
import gettext
import somemod
gettext.install(...)

#somemod.py
print _("text")

I'm not sure what sort of heuristic could be used to recognize _() when pyflake checks somemod.py.
Perhaps a command line argument for extra builtins?

Related branches

Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

Instead of using the injected builtins feature of gettext, import the name. Then both pyflakes and human readers of the code will know what's going on.

    from gettext import gettext as _

Changed in pyflakes:
status: New → Won't Fix
Revision history for this message
Dima Tisnek (dimaqq) wrote :

I'm not the expert on gettext, but it occurs to me there could be a subtle difference.
At least:

In [18]: __builtins__._ == gettext.gettext
Out[18]: False

In [19]: __builtins__._
Out[19]: <bound method GNUTranslations.gettext of <gettext.GNUTranslations instance at 0x1661d40>>

In [20]: gettext.gettext
Out[20]: <function gettext at 0x13df938>

I get the same translations from calling either function, and the proposed workaround works for me, but it's not always the case:

From gettext doc:
gettext.install(domain[, localedir[, unicode[, codeset[, names]]]])
Changed in version 2.4: Added the codeset parameter.
Changed in version 2.5: Added the names parameter.

Thus if someone uses extra parameters of install, result of _() and gettext.gettext() are different:

In [34]: gettext.install(APP, DIR, unicode=True)

In [36]: __builtins__._("test")
Out[36]: u'test'

In [37]: gettext.gettext("test")
Out[37]: 'test'

Here difference is unicode vs str, that is _() includes unicode=True passed in install and gettext.gettext doesn't. I suppose similar differences could arise from codeset and names parameters to install.

Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

The difference isn't really relevant to Pyflakes. The example I gave was meant to be a trivial example of how you can apply the solution. If you want a different gettext function to be bound to _, you can have a different gettext function bound to _. If you want unicode, use gettext.ugettext. If you want a particular domain, use the gettext.translation function to get an instance of something for the appropriate domain and then use that object's gettext method. etc.

Revision history for this message
Kovid Goyal (kovid) wrote :

There are plenty of situations where large projects may decide to define project wide builtins, in order to save having to explicitly import a frequently used function in every single module. Please consider adding support for a user specified list of builtin names to ignore, either via a command line argument or an environment variable.

Revision history for this message
Kovid Goyal (kovid) wrote :

And for completeness, here's a wrapper script that uses the env var PYFLAKES_BUILTINS

#!/usr/bin/env python
import os, __builtin__

names = os.environ.get('PYFLAKES_BUILTINS', '')
names = [x.strip() for x in names.split(',')]
for x in names:
    if not hasattr(__builtin__, x):
        setattr(__builtin__, x, True)

del names, os, __builtin__

from pyflakes.scripts.pyflakes import main
main()

Revision history for this message
Florent (florent.x) wrote :

The version 0.6.1 added a parameter to the Checker constructor.
See issue 1107587: http://launchpad.net/bugs/1107587

An environment variable might be sensible for the command line usage.

Changed in pyflakes:
importance: Undecided → Wishlist
status: Won't Fix → Confirmed
Florent (florent.x)
Changed in pyflakes:
milestone: none → 0.7.x
status: Confirmed → In Progress
Revision history for this message
Florent (florent.x) wrote :

Added environment variable PYFLAKES_BUILTINS as suggested. See NEWS.txt

Changed in pyflakes:
status: In Progress → Fix Committed
Florent (florent.x)
Changed in pyflakes:
milestone: 0.7.x → 0.7.2
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.