Skip to main content
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

Metadata

Release files for flake8-builtins 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-builtins 2.2.0
File Size Uploaded
flake8_builtins-2.2.0.tar.gz 17.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-builtins 2.2.0
File Interpreter ABI Platform
flake8_builtins-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.0 kB

Release files / flake8_builtins-2.2.0.tar.gz

Download URL flake8_builtins-2.2.0.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
392d5af3a0720c5a863aa93dc47f48c879081345a143fe9f20d995fe9ff5686a
BLAKE2b-256 checksum
How to use checksums
9bb95d14821917127d374523978fd2c31a70f771bb8510cccf57b3cc32c3990d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release files / flake8_builtins-2.2.0-py3-none-any.whl

Download URL flake8_builtins-2.2.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ee5766d9c60e5d579dfda84e65c6d0e6c26005f6f59cb9bf722462d7987a807
BLAKE2b-256 checksum
How to use checksums
14c65e4cbc348b7509b1fe7fafe322a9c9f2155e1307331e5b00c3d02520b2fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

This release

2.2.0 This release

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0

2 release files

0.4

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page