-
Notifications
You must be signed in to change notification settings - Fork 179
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
Comments
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. In [18]: builtins._ == gettext.gettext In [19]: builtins._ In [20]: gettext.gettext 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: 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") In [37]: gettext.gettext("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. |
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. |
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. |
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 names = os.environ.get('PYFLAKES_BUILTINS', '') del names, os, builtin from pyflakes.scripts.pyflakes import main |
Original comment by florent.x (@florentx?) on Launchpad: The version 0.6.1 added a parameter to the Checker constructor. An environment variable might be sensible for the command line usage. |
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:
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?
The text was updated successfully, but these errors were encountered: