Skip to main content

Check for python builtins being used as variables or parameters.

Project description

https://github.com/gforcada/flake8-builtins/actions/workflows/testing.yml/badge.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

Options

One can use –builtins-ignorelist option, or configuration option, to ignore a custom list of builtins:

$ flake8 --builtins-ignorelist id,copyright *.py

Requirements

  • Python 3.7, 3.8, 3.9, 3.10, 3.11, and pypy3

  • flake8

License

GPL 2.0

Changelog

2.0.1 (2022-11-01)

  • Add official support for python 3.10 and 3.11. [gforcada] [michael-k]

  • Update options handling. [gforcada]

  • Simplify testing. [gforcada]

2.0.0 (2022-10-08)

  • Mark support for python 3.9. [lovetoburnswhen]

  • Replace travis for GitHub. [cclauss]

  • Fix CI badge. [sobolevn]

  • Make black and isort mandatory. [cclauss]

  • Drop python 2.7 and 3.6. [gforcada]

  • Overhaul GitHub actions to test on actual supported python versions. [gforcada]

  • New flake8 option –builtins-ignorelist to specify a list of builtins to ignore. [gsingh93]

1.5.3 (2020-05-14)

  • Change messages to have shorter text

1.5.2 (2020-03-16)

  • Add DictComp, SetComp, and GeneratorExpr support. [sobolevn]

1.5.1 (2020-03-16)

  • Adds kwonly and posonly args support. [sobolevn]

1.5.0 (2020-03-14)

  • Add python3.8 support. [sobolevn]

  • Add AnnAssign support. [soboelvn]

  • Add NamedExpr or := support. [sobolevn]

1.4.2 (2019-12-18)

  • Fix tuple parsing. [memery-imb]

1.4.1 (2018-05-11)

  • Fix regression in 1.4.0 and interaction with flake8-bugbear. [dirk-thomas]

1.4.0 (2018-05-03)

  • Make code more robust by not assuming that a node is an ast.Name but actually checking it. [gforcada]

  • Handle ast.Starred as well (i.e. a, *int = range(4)) [gforcada]

  • Handle lists as well, i.e. [a, int] = 3, 4 [gforcada]

1.3.1 (2018-04-30)

1.3.0 (2018-04-13)

1.2.3 (2018-04-10)

1.2.2 (2018-04-03)

1.2.1 (2018-04-01)

  • re-relase 1.2 from master branch. [gforcada]

1.2 (2018-04-01)

  • Fix error message in for loops. [gforcada]

  • Inspect the following places for possible builtins being shadowed:

    • with open(‘/tmp/bla.txt’) as int

    • except ValueError as int

    • [int for int in range(4)]

    • from zope.component import provide as int

    • import zope.component as int

    • class int(object)

    • def int()

    • async def int()

    • async for int in range(4)

    • async with open(‘/tmp/bla.txt’) as int

    [gforcada]

1.1.1 (2018-03-20)

  • Variables assigned in a for loop can be not only a Tuple, but a Tuple inside a Tuple. [dopplershift]

1.1.0 (2018-03-17)

  • Update more trove classifiers. [gforcada]

  • Inspect variables assigned in a for loop as well. Thanks to sobolevn for reporting it! [gforcada]

1.0.post0 (2017-12-02)

  • Update README. [DmytroLitvinov]

  • Update trove classifiers. [dirn]

1.0 (2017-08-19)

  • Use requirements.txt to pin dependencies. [gforcada]

  • Fix tests with newer flake8 version. [gforcada]

  • BREAKING CHANGE: error codes have been changed from B00X to A00X to not clash with flake8-bugbear, see https://github.com/gforcada/flake8-builtins/issues/7 [gforcada]

0.4 (2017-05-29)

  • Use a different code for class attributes. [karamanolev]

0.3.1.post0 (2017-05-27)

  • Release universal wheels, not only python 2 wheels. [gforcada]

  • Update trove classifiers. [gforcada]

0.3.1 (2017-05-27)

  • Fix stdin handling. [sangiovanni]

0.3 (2017-05-15)

  • Handle stdin, which is the way flake8 gets integrated into editors. [gforcada]

  • Test against Python 2.7, 3.5, 3.6 and pypy. [gforcada]

0.2 (2016-03-30)

  • Whitelist some builtins. [gforcada]

0.1 (2016-03-04)

  • Initial release [gforcada]

  • Add buildout and other stuff. [gforcada]

  • Add actual code. [gforcada]

  • Drop support for python 3.3, only python 2.7 and python 3.4 are tested. [gforcada]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-builtins-2.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_builtins-2.0.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file flake8-builtins-2.0.1.tar.gz.

File metadata

  • Download URL: flake8-builtins-2.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.15

File hashes

Hashes for flake8-builtins-2.0.1.tar.gz
Algorithm Hash digest
SHA256 5aeb420130efe8acbdaf8708a582492413293a3ca25653518f687937879650a5
MD5 cca0da691cadfe3728724ea5cf263d40
BLAKE2b-256 2f465e030b1d51bb65c3fd2de82bc11830b62db8ffc222909493900771925b6d

See more details on using hashes here.

File details

Details for the file flake8_builtins-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_builtins-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5b9ca9cbc921c4455ea02e2e9963c990ac66d028c15b654625e012a1e3bbb4d
MD5 6c1c38b1bb02e4b470641f31e771702e
BLAKE2b-256 03f14becf5484ba165812fed14748e8e4ef709f5215a028bed2a234f3fcaa81f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page