Check for python builtins being used as variables or parameters

Overview
https://travis-ci.org/gforcada/flake8-builtins.svg?branch=master https://coveralls.io/repos/gforcada/flake8-builtins/badge.svg?branch=master

Flake8 Builtins plugin

Check for python builtins being used as variables or parameters.

Imagine some code like this:

def max_values(list, list2):
    max = list[0]
    for x in list:
        if x > 0:
            max = x

    all_values = list()
    all_values.append(max)

    max = list2[0]
    for x in list2:
        if x > 0:
            max = x
    all_values.append(max)

    return all_values

max_values([3, 4, 5, ], [5, 6, 7])

The last statement is not returning [5, 7] as one would expect, instead is raising this exception:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    max_values([3,4,5], [4,5,6])
  File "bla.py", line 6, in max_values
    all_values = list()
TypeError: 'list' object is not callable

Why? Because max_value function's first argument is list a Python builtin. Python allows to override them, but although could be useful in some really specific use cases, the general approach is to not do that as code then can suddenly break without a clear trace.

Example

Given the following code:

def my_method(object, list, dict):
    max = 5
    min = 3
    zip = (4, 3)

The following warnings are shown (via flake8):

test.py:1:15: A002 argument "object" is shadowing a python builtin
test.py:1:23: A002 argument "list" is shadowing a python builtin
test.py:1:29: A002 argument "dict" is shadowing a python builtin
test.py:2:5: A001 variable "max" is shadowing a python builtin
test.py:3:5: A001 variable "min" is shadowing a python builtin
test.py:4:5: A001 variable "zip" is shadowing a python builtin

Install

Install with pip:

$ pip install flake8-builtins

Requirements

  • Python 2.7, 3.6, 3.7, 3.8
  • flake8

License

GPL 2.0

Comments
  • Split A002 to differentiate between arguments and class-level variables

    Split A002 to differentiate between arguments and class-level variables

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

    opened by clokep 12
  • Traceback from flake8-bugbear

    Traceback from flake8-bugbear

    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
    
    opened by The-Compiler 10
  • Add option to ignore certain builtins

    Add option to ignore certain builtins

    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.

    opened by 0xallie 8
  • Tuple unpacking breaks this plugin

    Tuple unpacking breaks this plugin

    Code

    Valid python code break this linter with an exception.

    >>> a, *(b, c) = 1, 2, 3
    >>> print(a, b, c)
    1 2 3
    

    Problem

    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:

    • python3.7
    • flake8: 3.7.8
    • flake8-builtins: 1.4.1
    opened by sobolevn 8
  • Fix/tuple parsing

    Fix/tuple parsing

    This PR attempts to fix https://github.com/gforcada/flake8-builtins/issues/40.

    I added the test case mentioned and fixed the issue. Also, added a library called hypothesmith which generates thousands of parseable python programs. Because of this, I was able to find the same issue in the for loop code. If you like, you can cherry-pick flake8_builtins.py or include the new test and dependencies.

    Note, I had to update pytest, pluggy and attrs in the dev-dependencies.

    Happy holidays!

    opened by memery-rbx 7
  • `AttributeError` seen with versions 1.2.0 and 1.2.1

    `AttributeError` seen with versions 1.2.0 and 1.2.1

    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'
    
    opened by tim-ireland 7
  • Major cleanup: python versions, versions pinned and github actions

    Major cleanup: python versions, versions pinned and github actions

    Finally I have some time to spend on setting a new base for flake8-builtins future ✨

    • python 2.7 and 3.6 support was removed, which allows to remove quite some code
    • version pins on requirements.txt were updated, which will remove the security warnings GitHub is displaying
    • GitHub actions was updated, to ensure the supported python versions are actually working

    I always like to create lots of commits with each doing one specific topic, so although the changes are quite a lot, they are all (hopefully) isolated 🀞🏾 πŸ˜„

    @cclauss @gsingh93 you might want to have a look. I will review and merge your PR on top of this one, if I manage to make this one pass CI, my skills level on GitHub actions is rather low πŸ˜“ today is already improving this πŸ˜„

    opened by gforcada 5
  • Does not recognize additional --builtins

    Does not recognize additional --builtins

    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.

    opened by kthy 5
  • Remove '_' from the builtins list

    Remove '_' from the builtins list

    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.

    opened by sobolevn 5
  • False negative in some control flows

    False negative in some control flows

    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.

    opened by sobolevn 5
  • Broken under SublimeLinter-flake8

    Broken under SublimeLinter-flake8

    Commit 628e988459fd827e4960ea82a397a08914a5de23 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.

    opened by danpalmer 5
  • A003 should be disabled by default

    A003 should be disabled by default

    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.

    opened by wyuenho 6
Flake8 extension to provide force-check option

flake8-force Flake8 extension to provide force-check option. When this option is enabled, flake8 performs all checks even if the target file cannot be

Kenichi Maehashi 9 Oct 29, 2022
Simple Python style checker in one Python file

pycodestyle (formerly called pep8) - Python style guide checker pycodestyle is a tool to check your Python code against some of the style conventions

Python Code Quality Authority 4.7k Jan 1, 2023
Optional static typing for Python 3 and 2 (PEP 484)

Mypy: Optional Static Typing for Python Got a question? Join us on Gitter! We don't have a mailing list; but we are always happy to answer questions o

Python 14.4k Jan 8, 2023
A Python Parser

parso - A Python Parser Parso is a Python parser that supports error recovery and round-trip parsing for different Python versions (in multiple Python

Dave Halter 520 Dec 26, 2022
A simple program which checks Python source files for errors

Pyflakes A simple program which checks Python source files for errors. Pyflakes analyzes programs and detects various errors. It works by parsing the

Python Code Quality Authority 1.2k Dec 30, 2022
Performant type-checking for python.

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providi

Facebook 6.2k Jan 4, 2023
A static type analyzer for Python code

pytype - ?? βœ” Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 4k Dec 31, 2022
The strictest and most opinionated python linter ever!

wemake-python-styleguide Welcome to the strictest and most opinionated python linter ever. wemake-python-styleguide is actually a flake8 plugin with s

wemake.services 2.1k Jan 1, 2023
Static type checker for Python

Static type checker for Python Speed Pyright is a fast type checker meant for large Python source bases. It can run in a β€œwatch” mode and performs fas

Microsoft 9.2k Jan 3, 2023
A python documentation linter which checks that the docstring description matches the definition.

Darglint A functional docstring linter which checks whether a docstring's description matches the actual function/method implementation. Darglint expe

Terrence Reilly 463 Dec 31, 2022
Flake8 plugin that checks import order against various Python Style Guides

flake8-import-order A flake8 and Pylama plugin that checks the ordering of your imports. It does not check anything else about the imports. Merely tha

Python Code Quality Authority 270 Nov 24, 2022
Flake8 extension for checking quotes in python

Flake8 Extension to lint for quotes. Major update in 2.0.0 We automatically encourage avoiding escaping quotes as per PEP 8. To disable this, use --no

Zachary Heller 157 Dec 13, 2022
flake8 plugin to run black for checking Python coding style

flake8-black Introduction This is an MIT licensed flake8 plugin for validating Python code style with the command line code formatting tool black. It

Peter Cock 146 Dec 15, 2022
Custom Python linting through AST expressions

bellybutton bellybutton is a customizable, easy-to-configure linting engine for Python. What is this good for? Tools like pylint and flake8 provide, o

H. Chase Stevens 249 Dec 31, 2022
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. β€” The Jungle Book.

beartype 1.4k Jan 1, 2023
Naming Convention checker for Python

PEP 8 Naming Conventions Check your code against PEP 8 naming conventions. This module provides a plugin for flake8, the Python code checker. (It repl

Python Code Quality Authority 411 Dec 23, 2022
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) Β© 2012-2013, Florent Xicluna; pydocstyle (form

Kirill Klenov 967 Jan 7, 2023
Flake8 extension for enforcing trailing commas in python

Flake8 Extension to enforce better comma placement. Usage If you are using flake8 it's as easy as: pip install flake8-commas Now you can avoid those a

Python Code Quality Authority 127 Sep 3, 2022
Tool for pinpointing circular imports in Python. Find cyclic imports in any project

Pycycle: Find and fix circular imports in python projects Pycycle is an experimental project that aims to help python developers fix their circular de

Vadim Kravcenko 311 Dec 15, 2022