gforcada / flake8-builtins Goto Github PK
View Code? Open in Web Editor NEWCheck for python builtins being used as variables or parameters
Home Page: https://pypi.org/project/flake8-builtins
License: GNU General Public License v2.0
Check for python builtins being used as variables or parameters
Home Page: https://pypi.org/project/flake8-builtins
License: GNU General Public License v2.0
I have spotted that this example gives me false negative.
No errors is created, but there should be one.
def validate_date(date: str) -> datetime:
"""
Validates that value is a valid date.
>>> validate_date('20.12.2010')
datetime.datetime(2010, 12, 20, 0, 0, tzinfo=<UTC>)
"""
valid_formats = [
'%d.%m.%y',
'%d.%m.%Y',
'%e.%m.%y',
'%e.%m.%Y',
]
for format in valid_formats: # format is built-in name, but no error
try:
return datetime.strptime(date, format) # no error here either
except ValueError:
continue
format
is a built-in name that is not checked for some reason.
Given this code sample:
try:
1 / 0
except ZeroDivisionError as sum:
print(sum)
This should rise an error. But it does not.
That's a bug, I guess.
I will create a PR with the test after #15 will be merged.
Python 2 died more than 1,000 days ago on 1/1/2020. We should drop support for all versions of Python < v3.7.
I just updated this package from 1.3.1 to 1.4.0 and experienced a large number of F821 undefined name ...
. Reverting back to 1.3.1 resolves the problem again.
Looking into the recent CI results, e.g. for 92e51e6 it seems to show the same problem (see https://travis-ci.org/gforcada/flake8-builtins/jobs/374539041).
To test the code against the latest released version of flake-builtins
and demonstrate its usage.
This line only checks ast.ListComp
:
flake8-builtins/flake8_builtins.py
Line 106 in 225888c
Code: [list for list in '']
Outout:
» flake8 ex.py
ex.py
1:1 A001 "list" is a python builtin and is being shadowed, consider renaming the variable
[list for list in '']
^
We need to add support for other types of comprehensions.
Because, these types do not raise:
{list for list in ''}
(list for list in '')
{list: 1 for list in ''}
Would it be possible to update setup.py to include python 3.9?
Thanks
There are several things to add:
ast.NamedExpr
supportpython3.8
to the test casepython3.8
classifiers(list := 1)
The lines below overwrite built-ins and can introduce surprising bugs, but they are not flagged:
from numpy import all, any, sum
from numpy import allclose as close
Can we get a new rule that catches this?
We are working a lot with histograms, which means we are talking/coding a lot about "bins".
And in the code they are named as such and i think it is best for readability of we do keep that name.
However i would still like to check all other cases of shadowing builtins. Is it possible to disable the checks just for this one builtin without disabeling A001 completely?
Edit: Does exist via --builtins-ignorelist
https://pypi.org/project/pytest is currently v7.1.4
requirements.txt
specifies pytest==5.4.1
which cannot even do pytest --version
on Python 3.10.
Similar to how check_import()
compares to names of builtins
but this time covering packages like logging
or secrets
which is a common python gotcha.
The list of these packages exists as the sys.stdlib_module_names
tuple from 3.10, so I hope 🙏 we could trivially support this rule in python 3.10+ to begin with.
Some example references:
Reproduction:
echo 'list: int = 1' > ex.py
flake8 ex.py
It exists with status code 0
.
I expect it to work the same as list = 1
which produces:
» flake8 ex.py
ex.py
1:1 A001 "list" is a python builtin and is being shadowed, consider renaming the variable
list = 1
^
The problem is in this line: https://github.com/gforcada/flake8-builtins/blob/master/flake8_builtins.py#L81 Because it does not check ast.AnnAssign
With flake8-builtins 1.3.0 and flake8 3.5.0, I'm getting the following error on Python 2.7:
Traceback (most recent call last):
File "/home/ubuntu/virtualenvs/venv-system/bin/flake8", line 11, in <module>
sys.exit(main())
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/main/application.py", line 396, in run
self._run(argv)
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/main/application.py", line 385, in _run
self.report()
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/main/application.py", line 376, in report
self.report_errors()
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/main/application.py", line 340, in report_errors
results = self.file_checker_manager.report()
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/checker.py", line 267, in report
results_reported += self._handle_results(filename, results)
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/checker.py", line 166, in _handle_results
physical_line=physical_line,
File "/home/ubuntu/virtualenvs/venv-system/local/lib/python2.7/site-packages/flake8/style_guide.py", line 384, in handle_error
error = Violation(code, filename, line_number, column_number + 1,
TypeError: cannot concatenate 'str' and 'int' objects
This seems to be while reporting the error? This seems new in 1.3.0, no error in 1.2.1.
I've just added this to my project, and it has caught a number of my mistakes... but it's also catching places where outside libraries chose kwargs that shadow builtins, which doesn't worry me as much: I would probably ignore these. Both cases raise A002
: Would it make sense to split this into two categories?
I can try to make a PR, but I wanted to make sure this is a reasonable direction, first.
Consider the following example:
class User(Model):
id: str
def __str__(self):
return f"User(id:{self.id})"
user = User(id='123')
print(user.id)
Class attributes are almost never accessed by themselves without going through an object or a class, so what's the point of this rule? If you must preserve this rule, please disable it by default.
As per the docs, it is possible to add builtins to flake8 by passing the --builtins
flag or through config files.
However, these are not recognized by flake8-builtins:
import gettext
gettext.install("minimal_repro")
def id(s):
return s
def _(s):
return s
print(id("foo"))
print(_("bar"))
$ python ./no_custom_builtins.py
foo
bar
$ flake8 ./no_custom_builtins.py --builtins=_
./no_custom_builtins.py:1:1: D100 Missing docstring in public module
./no_custom_builtins.py:6:1: A001 variable "id" is shadowing a python builtin
./no_custom_builtins.py:6:1: D103 Missing docstring in public function
A001 should be reported for both functions.
I just upgraded to v1.2, and I feel like it's generating way too many false-positives.
First of all: I haven't thought about the full list of cases detected in the changelog yet, but in general, every of those cases should get a dedicated error code, so it's easy to silence a given kind of warning via flake8 if it's a hindrance for some projects.
What is the problem in my case specifically is class methods: For example, if I want to extend Python's logging
module, I'll need to subclass things from there and override methods called filter
or format
.
I'd even go further and argue class methods shouldn't be checked at all by default - it's way too likely to bring up issues which can't be fixed, and the name is only shadowed in the class body, where's typically not any code other than function/variable definitions at all.
flake8-builtins complains about variables named license
, which honestly I don't understand why it even exists outside of the REPL. I don't want to rename them to lic
, licence
, licence_
or similar, so it would be nice to not have to sprinkle my code with # noqa
everywhere.
In version 1.4.1:
def id(self): # noqa: D102, A003
raises a flake8 violation although it is explicitly ignored.
A003 "id" is a python builtin, consider renaming the class attribute
I find this plugin very useful, especially for module-level globals, function arguments, variables, etc., but class attributes are really getting in the way. Can you add a configuration option to disable the errors just for that? There are a bunch of legitimate cases where we have class attributes names that are builtins:
Hi,
i'm trying to use builtins on VSCode without success. Using Python 3.10. Is this version not supported yet?
Thanks!
The badge in the readme / on PyPI says that the build is failing
With version flake8-builtins 1.2.0 and later I'm seeing the following issue while running flake8
. It works fine with flake8-builtins 1.1.1.
Using python 2.7.14
[root@532b769a6003 /]# flake8 src/
Traceback (most recent call last):
File "/opt/stackstorm/virtualenvs/stackstorm/bin/flake8", line 11, in <module>
sys.exit(main())
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/main/application.py", line 328, in run
self._run(argv)
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/main/application.py", line 316, in _run
self.run_checks()
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/main/application.py", line 246, in run_checks
self.file_checker_manager.run()
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/checker.py", line 317, in run
self.run_parallel()
File "/opt/stackstorm/virtualenvs/stackstorm/lib/python2.7/site-packages/flake8/checker.py", line 286, in run_parallel
for ret in pool_map:
File "/opt/python/lib/python2.7/multiprocessing/pool.py", line 289, in <genexpr>
return (item for chunk in result for item in chunk)
File "/opt/python/lib/python2.7/multiprocessing/pool.py", line 673, in next
raise value
AttributeError: 'Attribute' object has no attribute 'id'
After upgrading to the latest version, I have faced this issue:
/Users/.../user_details_step.py:3:1: A001 "_" is a python builtin and is being shadowed,
consider renaming the variable
But _
is a common name for unused variables, I think it is not right to annotate them with # noqa
.
E.g. with the latest version (1.2.1) I have a few examples of things like this:
class MyClass(object):
def filter(self, a, b):
pass
This now fails with a message:
A002 "filter" is used as an argument and thus shadows a python builtin, consider renaming the argument
This text doesn't really make sense (filter
isn't an argument).
It looks like the master
branch on GitHub still exists. Thus, links like e.g. https://github.com/gforcada/flake8-builtins/blob/master/CHANGES.rst point to an outdated version of the file.
When deleting it, GitHub will instead automatically redirect such links from master to main and show a banner about the move.
After upgrading to 1.2.2 we are getting this error:
Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/ve/lib/python3.6/site-packages/flake8/checker.py", line 648, in _run_checks
return checker.run_checks()
File "/ve/lib/python3.6/site-packages/flake8/checker.py", line 579, in run_checks
self.run_ast_checks()
File "/ve/lib/python3.6/site-packages/flake8/checker.py", line 493, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/ve/lib/python3.6/site-packages/flake8_builtins.py", line 103, in run
for line, offset, msg, rtype in value:
File "/ve/lib/python3.6/site-packages/flake8_builtins.py", line 192, in check_with
if var and var.id in BUILTINS:
AttributeError: 'Tuple' object has no attribute 'id'
This is caused by something like:
with my_manager() as (a, b):
....
Not sure if this intended, but A005 does not fire for packages (folders) shadowing stdlib modules. So, socket/__init__.py
will not lead to an flake8 error
Valid python
code break this linter with an exception.
>>> a, *(b, c) = 1, 2, 3
>>> print(a, b, c)
1 2 3
Reproduction:
# test.py
a, *(b, c) = 1, 2, 3
Run: flake8 test.py
Output:
» flake8 test.py
Traceback (most recent call last):
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/bin/flake8", line 10, in <module>
sys.exit(main())
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/main/application.py", line 381, in _run
self.run_checks()
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/main/application.py", line 300, in run_checks
self.file_checker_manager.run()
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/checker.py", line 331, in run
self.run_serial()
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/checker.py", line 315, in run_serial
checker.run_checks()
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8/checker.py", line 502, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8_builtins.py", line 106, in run
for line, offset, msg, rtype in value:
File "/Users/sobolev/Documents/github/wemake-python-styleguide/.venv/lib/python3.7/site-packages/flake8_builtins.py", line 124, in check_assignment
item.value.id in BUILTINS:
AttributeError: 'Tuple' object has no attribute 'id'
Versions:
Hi, thank you for the awesome plugin!
It's helpful for me to identify if I shadow built-in function in the global or local scope.
But class methods and class attributes are somewhat different from that. Class methods do not hide anything from the global or local scope since we should refer to self
before.
There are even situations where we are forced to write code like that. For example, Django management commands require a help
class attribute. Django REST Framework requires list
method on APIViews and ViewSets.
I don't want to lose the ability to find the redefinition of int
in these files. I don't want to white noqa
on each line with class attributes neither.
It would be helpful to be able to turn off that particular behavior.
What do you think?
Have a good day! 🎉
Best regards,
Artem.
Commit 628e988 broke flake8-builtins
when running in SublimeText under SublimeLinter and SublimeLinter-flake8. This causes flake8 linting to stop working entirely in the editor as an exception is thrown during linting.
Traceback (most recent call last):
File "~/.virtualenvs/linting/bin/flake8", line 11, in <module>
sys.exit(main())
File "~/.virtualenvs/linting/lib/python2.7/site-packages/flake8/main.py", line 36, in main
report = flake8_style.check_files()
File "~/.virtualenvs/linting/lib/python2.7/site-packages/flake8/engine.py", line 181, in check_files
return self._retry_serial(self._styleguide.check_files, paths=paths)
File "~/.virtualenvs/linting/lib/python2.7/site-packages/flake8/engine.py", line 172, in _retry_serial
return func(*args, **kwargs)
File "~/.virtualenvs/linting/lib/python2.7/site-packages/pycodestyle.py", line 1877, in check_files
runner(path)
File "~/.virtualenvs/linting/lib/python2.7/site-packages/flake8/engine.py", line 126, in input_file
return fchecker.check_all(expected=expected, line_offset=line_offset)
File "~/.virtualenvs/linting/lib/python2.7/site-packages/pycodestyle.py", line 1608, in check_all
self.check_ast()
File "~/.virtualenvs/linting/lib/python2.7/site-packages/pycodestyle.py", line 1555, in check_ast
for lineno, offset, text, check in checker.run():
File "~/.virtualenvs/linting/lib/python2.7/site-packages/flake8_builtins.py", line 54, in run
tree = ast.parse(lines)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
TypeError: expected a readable buffer object
I'm not sure exactly what is going wrong here, but apparently dropping the lines added in the commit fixes the issue. I don't want to just make a PR for this though, as I'm sure I'm missing some detail as to why it was added in the first place.
I'm getting this error now that I updated to 1.1.0:
Traceback (most recent call last):
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/flake8/checker.py", line 648, in _run_checks
return checker.run_checks()
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/flake8/checker.py", line 579, in run_checks
self.run_ast_checks()
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/flake8/checker.py", line 493, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/flake8_builtins.py", line 73, in run
for line, offset, msg, rtype in value:
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/flake8_builtins.py", line 129, in check_for_loop
if name.id in BUILTINS:
AttributeError: 'Tuple' object has no attribute 'id'
I'm seeing this on Windows, Linux, and Mac on 2.7, 3.5, 3.6. This example code (which pulls a tuple out during iteration) is enough to trigger the error (and removing that line makes the error go away):
a = [('a', 'b'), ('c', 'd')]
for i, (x, y) in enumerate(a):
print(i, x, y)
See #22 (comment) for the details.
So far it seems that the ast
module does not provide that information.
An upstream bug to CPython has been created for that: https://bugs.python.org/issue33211
/cc @dhood
When using with a TypeDict like:
class PaginationDict(TypedDict):
prev: Optional[str]
next: Optional[str]
count: int
data: any
This will incorrectly flag: A003 class attribute "next" is shadowing a Python builtin
These checks should be disabled when a class inherits from TypedDict
I think having a flag to enable shorter warning messages would be nice. I am using vscode and the warnings are extremely long:
Something like variable "max" is shadowing a python builtin
would be plenty to convey the same meaning and cut down the length of the warning by almost half and changing the argument message to argument "object" is shadowing a python builtin
would make it 50 characters shorter
There's a problem: you have two licenses for this project.
My guess is that LICENSE.rst
is not required.
This code does not raise any violations:
def some(list, /, arg):
...
Because /
posonly args are not checked.
It is a thing since 3.8
I will send a PR after #52 is merged
class StudentListSerializer(serializers.ModelSerializer):
id = serializers.IntegerField()
username = serializers.CharField()
email = serializers.CharField()
first_name = serializers.CharField()
last_name = serializers.CharField()
date_joined = serializers.CharField()
avatar = S3DirectUploadURLField()
The plugin will alarm
apps/users/api/student/serializers.py:19:5: A003 "id" is a python builtin, consider renaming the class attribute
is there a configuration option to prevent checking for built-ins for class attrs?
Thanks,
Dmitry
I have not tested the consequences of this but I found googling for B001 that you both picked the same B prefix code in March 2016!
Hello,
this is the third time I am submitting this bug, hopefully this is the right place, @jack-mcivor directed me here from PyCQA/pyflakes#787 , before I submitted it to flake8 repo...
I am not sure if this belongs to this plugin and I'm not sure how to bite this exactly, but I have feeling somewhere in the back of my head, that this should be made visible to the end-user, aka programmer.
If this does not belong to this plugin, please help me and show me the direction I should take it to.
I recently made a somewhat funny bug in my codebase.
It looked like this:
SOME_CONST = f"http://some.host/{id}"
The bug-introducing character was that little "f" before the starting quote.
I meant to declare a string, that gets .format
'ted some time later (like first I defined the URL with the place for the parameters, then I wanted to do SOME_CONST.format({"id": "some_id"})
)
Instead, I declared a constant that looked like http://some.host/<builtin function id>
. To add to the injury, .format()
on that constant worked without problems at all, obviously.
First I wonder what would I like to do with that. We already have a warning for when there are f-string format characters and the string is missing the starting "f".
This would be the opposite, but only for some symbols.
On the other hand, there is very little chance somebody would want to have a string-description of a function embedded in a string constant.
So, warning about a variable found in f-string not declared in the local scope could be okay, but I am not sure if this is even possible. That SOME_CONST
was declared at the top level of the module, all the imported things and symbols were available. Of course that id()
was in the builtins, not in the module scope.
Maybe it would be sensible to warn the user about converting function-type object to a string in a string constant. I think that this is not the thing people like to do on the daily basis.
I would like to know your opinion on this problem first.
Any opinions?
The plugin doesn't check code issues if flake8 runs via pflake8.
Other flake8 plugins (like flake8-copyright, flake8-docstrings, pep8-naming) work fine.
Once I upgrade to flake8-builtins 1.4.0, I started seeing these errors:
metpy/calc/thermo.py:2024:28: B007 Loop control variable 'ret' not used within the loop body. If this is intended, start the name with an underscore.
These are actually generated by the flake8-bugbear (18.2.0) plugin, and are spurious. Installing flake8-builtins 1.3.1 causes them to go away, so I'm guessing the problem actually lies in flake8-builtins.
Similar to #35 I see some weird interaction with flake8-bugbear since upgrading flake8-builtins to 1.4.0. Here's what I get:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/opt/python/3.6.3/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/opt/python/3.6.3/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/checker.py", line 648, in _run_checks
return checker.run_checks()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/checker.py", line 579, in run_checks
self.run_ast_checks()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/checker.py", line 493, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/bugbear.py", line 37, in run
visitor.visit(self.tree)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/bugbear.py", line 143, in visit
super().visit(node)
File "/opt/python/3.6.3/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/opt/python/3.6.3/lib/python3.6/ast.py", line 261, in generic_visit
self.visit(item)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/bugbear.py", line 143, in visit
super().visit(node)
File "/opt/python/3.6.3/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/bugbear.py", line 235, in visit_ClassDef
self.check_for_b903(node)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/bugbear.py", line 414, in check_for_b903
if len(targets) > 1 or not isinstance(targets[0], ast.Attribute):
IndexError: list index out of range
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/python/3.6.3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/opt/python/3.6.3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/__main__.py", line 4, in <module>
cli.main()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/main/application.py", line 396, in run
self._run(argv)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/main/application.py", line 384, in _run
self.run_checks()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/main/application.py", line 310, in run_checks
self.file_checker_manager.run()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8_per_file_ignores.py", line 33, in run
orig_run(self)
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/checker.py", line 319, in run
self.run_parallel()
File "/home/travis/build/qutebrowser/qutebrowser/.tox/flake8/lib/python3.6/site-packages/flake8/checker.py", line 288, in run_parallel
for ret in pool_map:
File "/opt/python/3.6.3/lib/python3.6/multiprocessing/pool.py", line 347, in <genexpr>
return (item for chunk in result for item in chunk)
File "/opt/python/3.6.3/lib/python3.6/multiprocessing/pool.py", line 735, in next
raise value
IndexError: list index out of range
The source code
from requests.exceptions import ConnectionError
Raises A004 import statement "ConnectionError" is shadowing a Python builtin
.
I understand that ConnectionError is a builtin, which is a subclass of OSError. However, ConnectionError
is quite widely used term.
The more issue is, when we apply flake8-builtins in CI, we cannot make a new connection module which provides ConnectionError.
Can we have separate rules for such exception builtins?
While packaging the software for Debian I found that one of the test triggers a real exception. Environment is Debian Sid with python version is 3.9.2.
test_with_statement_unpack_on_list (run_tests.TestBuiltins) ... ok
test_with_statement_unpack_star (run_tests.TestBuiltins) ... ok
======================================================================
ERROR: test_exception_py2 (run_tests.TestBuiltins)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jrivero/code/debian/flake8-builtins/run_tests.py", line 329, in test_exception_py2
tree = ast.parse(
File "/usr/lib/python3.9/ast.py", line 50, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 3
except Exception, int:
^
SyntaxError: invalid syntax
----------------------------------------------------------------------
Ran 60 tests in 0.018s
FAILED (errors=1)
Please note that I'm running the test suite with the following patch (since hypothesmith is not in Debian). I'm not sure that the error is related:
diff --git a/run_tests.py b/run_tests.py
index 3ee8091..a0594e6 100644
--- a/run_tests.py
+++ b/run_tests.py
@@ -16,7 +16,6 @@ except ImportError:
if sys.version_info >= (3, 6):
from hypothesis import given
from hypothesis import reject
- import hypothesmith
class TestBuiltins(unittest.TestCase):
@@ -570,7 +569,6 @@ class TestBuiltins(unittest.TestCase):
if sys.version_info >= (3, 6):
- @given(source_code=hypothesmith.from_grammar())
def test_builtin_works_on_many_examples(source_code):
try:
source = source_code.encode('utf-8-sig')
Thanks.
Nice idea, I wish I could use it on my current project however there are a lot of class attributes (methods and variables) that are deemed acceptable to use builtin names.
# test_set.py
class MyClass:
def set(self, value):
pass
instance = MyClass()
instance.set("does not colide with builtins")
$ flake8 test_set.py
test_set.py:2:5: A003 "set" is a python builtin, consider renaming the class attribute
> pip3 uninstall flake8-builtins
> flake8
ERROR:flake8.plugins.manager:No module named 'flake8_builtins'
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 158, in load_plugin
self._load()
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 135, in _load
self._plugin = self.entry_point.load()
File "/home/ubuntu/.local/lib/python3.6/site-packages/entrypoints.py", line 82, in load
mod = import_module(self.module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'flake8_builtins'
CRITICAL:flake8.plugins.manager:Flake8 failed to load plugin "A00" due to No module named 'flake8_builtins'.
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 158, in load_plugin
self._load()
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 135, in _load
self._plugin = self.entry_point.load()
File "/home/ubuntu/.local/lib/python3.6/site-packages/entrypoints.py", line 82, in load
mod = import_module(self.module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'flake8_builtins'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/.local/bin/flake8", line 11, in <module>
sys.exit(main())
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/main/application.py", line 380, in _run
self.initialize(argv)
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/main/application.py", line 363, in initialize
self.find_plugins()
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/main/application.py", line 199, in find_plugins
self.check_plugins.load_plugins()
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 410, in load_plugins
plugins = list(self.manager.map(load_plugin))
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 297, in map
yield func(self.plugins[name], *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 408, in load_plugin
return plugin.load_plugin()
File "/home/ubuntu/.local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 165, in load_plugin
raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "A00" due to No module named 'flake8_builtins'.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.