Skip to main content

Check for python builtins being used as variables or parameters.

Project description

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

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 "object" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:1:23: A002 "list" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:1:29: A002 "dict" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:2:5: A001 "max" is a python builtin and is being shadowed, consider renaming the variable
test.py:3:5: A001 "min" is a python builtin and is being shadowed, consider renaming the variable
test.py:4:5: A001 "zip" is a python builtin and is being shadowed, consider renaming the variable

Install

Install with pip:

$ pip install flake8-builtins

Requirements

  • Python 2.7, 3.5, 3.6

  • flake8

License

GPL 2.0

Changelog

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-1.4.2.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

flake8_builtins-1.4.2-py2.py3-none-any.whl (13.3 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: flake8-builtins-1.4.2.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for flake8-builtins-1.4.2.tar.gz
Algorithm Hash digest
SHA256 c44415fb19162ef3737056e700d5b99d48c3612a533943b4e16419a5d3de3a64
MD5 f1b55546dcb9be98a14ab8183f570098
BLAKE2b-256 78af83befff5520d1da5aa69a052f5371bac08b364f8ec8968552b0ac8297824

See more details on using hashes here.

File details

Details for the file flake8_builtins-1.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: flake8_builtins-1.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for flake8_builtins-1.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 29bc0f7e68af481d088f5c96f8aeb02520abdfc900500484e3af969f42a38a5f
MD5 9604e607e2711c3a74f9ca580a790e20
BLAKE2b-256 9b70df22f060e73cb062449d0307e9dfe0f306d09fb6399a2d4cfa5d87f14e34

See more details on using hashes here.

Supported by

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