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

  • flake8

License

GPL 2.0

Changelog

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

Uploaded Source

Built Distribution

flake8_builtins-0.3-py2-none-any.whl (5.9 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for flake8-builtins-0.3.tar.gz
Algorithm Hash digest
SHA256 5081ece3ed5af8b7cb1351dec3ec2d0ac47b3284c1663f79640f0cd20d7dcf67
MD5 6d029e6ede2db0f0281e26b961a7375a
BLAKE2b-256 546525db5fc7c6a3bc5accdc28c02373bb1ae15687dad2447291d730c0642624

See more details on using hashes here.

File details

Details for the file flake8_builtins-0.3-py2-none-any.whl.

File metadata

File hashes

Hashes for flake8_builtins-0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 c99c6494c78c493344bb4f45a778193cc1b7a3ecaabef4749b38481125338d86
MD5 5de122abf429b39a225a36cf9654b449
BLAKE2b-256 c4d45e46977b61832b644600b7b7dba69dae3fc6ec5412581671e9c8c2b5a5b4

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