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=main https://coveralls.io/repos/gforcada/flake8-builtins/badge.svg?branch=main

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:

$ python -m 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.8, 3.9, 3.10, 3.11, and pypy3

  • flake8

Rules

A001:

A variable is shadowing a Python builtin.

A002:

An argument is shadowing a Python builtin.

A003:

A class attribute is shadowing a Python builtin.

A004:

An import statement is shadowing a Python builtin.

License

GPL 2.0

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.2.0a0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

flake8_builtins-2.2.0a0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file flake8_builtins-2.2.0a0.tar.gz.

File metadata

  • Download URL: flake8_builtins-2.2.0a0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for flake8_builtins-2.2.0a0.tar.gz
Algorithm Hash digest
SHA256 0fd7b27159a9a1d0240176c6918fe49960afa60615e0a948438dc08fca4741c9
MD5 44d638fc85fc88a50eaae758c42b1e31
BLAKE2b-256 dc97015611643faf82de6ab85769f107c49b9230ea41bf575fe55a133fd50269

See more details on using hashes here.

File details

Details for the file flake8_builtins-2.2.0a0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_builtins-2.2.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 7406e4b2571cea2f9f11b431b67f02760a52a3540fedc9778b6861d84f83141f
MD5 4959bb7625dfe93c99bc13e4146e0f01
BLAKE2b-256 29b4b14e2727af702856f8d537e6be687f0917cd15b3414cdf6af49c8a2efb96

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