Skip to content
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

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

Closed
pyflakes-bot opened this issue Sep 8, 2011 · 7 comments
Closed

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

pyflakes-bot opened this issue Sep 8, 2011 · 7 comments

Comments

@pyflakes-bot
Copy link

Original report by dimaqq (@dimaqq?) on Launchpad:


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?

@pyflakes-bot
Copy link
Author

Original comment by exarkun (@exarkun?) on Launchpad:


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 _

@pyflakes-bot
Copy link
Author

Original comment by dimaqq (@dimaqq?) on Launchpad:


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.

@pyflakes-bot
Copy link
Author

Original comment by exarkun (@exarkun?) on Launchpad:


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.

@pyflakes-bot
Copy link
Author

Original comment by kovid on Launchpad:


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.

@pyflakes-bot
Copy link
Author

Original comment by kovid on Launchpad:


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()

@pyflakes-bot
Copy link
Author

Original comment by florent.x (@florentx?) on Launchpad:


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.

@pyflakes-bot
Copy link
Author

Original comment by florent.x (@florentx?) on Launchpad:


Added environment variable PYFLAKES_BUILTINS as suggested. See NEWS.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant