Comments (7)
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 _
from pyflakes.
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.
from pyflakes.
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.
from pyflakes.
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.
from pyflakes.
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()
from pyflakes.
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.
from pyflakes.
Original comment by florent.x (@florentx?) on Launchpad:
Added environment variable PYFLAKES_BUILTINS as suggested. See NEWS.txt
from pyflakes.
Related Issues (20)
- f-string like f"{foo = }" is detected as an error HOT 1
- Feature request: a rule for calling super().__init__() in custom exception's __init__() HOT 1
- undefined name/syntax error in forward annotation HOT 2
- Use setuptools.build_meta HOT 1
- pyflakes: Does not consider legacy type annotations for "usedness" of imports HOT 2
- F811 redefinition of unused '_' - should underscore be excluded? HOT 1
- Spurious F811 when a method in a class overrides an "unused import" HOT 1
- F821 for PEP695 ParamSpec HOT 3
- Docu: What does pyflake checks for? HOT 2
- False Positive: walrus in a list comprehension HOT 6
- E704 false positives with one line overload def HOT 1
- False-positive F811 HOT 1
- False positive F841 and F821 HOT 5
- pyflakes misses an undefined variable (F821) when a variable is defined in an outer scope, but not in time HOT 4
- Python 3.12: SyntaxError: source code string cannot contain null bytes HOT 2
- F821 undefined name 'something' when using __getitem__ HOT 9
- import * 误报的问题 The issue of import * false positives HOT 1
- Python 3.13.0b1: test_errors_syntax fail because of a changed error message HOT 1
- `test_errors_syntax` fails with Python 3.13.0b2
- Remove Python 2.x code? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyflakes.