flake8-builtins
flake8-builtins is a plugin for the Flake8 code checker that identifies when Python's built-in functions, types, or modules are used as variable names, function parameters, or class attributes, which can lead to unexpected runtime errors. The current version is 3.1.0, actively maintained with releases tied to new Python versions and features.
Warnings
- breaking Error codes changed from `B00X` to `A00X` in version 1.0 (released 2017-08-19). This was done to avoid clashes with `flake8-bugbear`.
- gotcha Shadowing built-in names (e.g., `list`, `dict`, `max`, `id`) can lead to subtle and hard-to-debug `TypeError` exceptions, as the built-in function/type becomes inaccessible. This is the primary problem `flake8-builtins` aims to solve.
- gotcha As of Python 3.10, `flake8-builtins` includes checks (A004, A005) for shadowing built-in module names (e.g., creating a file `logging.py` or `time.py` or importing a symbol as a builtin name).
- gotcha The `requires_python` metadata indicates compatibility with Python versions. Older Python versions (e.g., 3.7 and 3.8) are no longer supported by recent `flake8-builtins` releases.
Install
-
pip install flake8-builtins
Imports
- BuiltinsChecker
N/A
Quickstart
# my_module.py
def process_data(list, dict_obj):
# A002: argument "list" is shadowing a python builtin
# A002: argument "dict_obj" is shadowing a python builtin
max = 10 # A001: variable "max" is shadowing a python builtin
data = list([1, 2, 3])
return data
def my_func():
zip = __import__('zipfile') # A004: import statement "zip" is shadowing a Python builtin
# To run this, save it as `my_module.py` and execute in your terminal:
# pip install flake8 flake8-builtins
# flake8 my_module.py