Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gforcada/flake8-builtins
Check for python builtins being used as variables or parameters
https://github.com/gforcada/flake8-builtins
Last synced: 2 days ago
JSON representation
Check for python builtins being used as variables or parameters
- Host: GitHub
- URL: https://github.com/gforcada/flake8-builtins
- Owner: gforcada
- License: gpl-2.0
- Created: 2016-03-02T00:26:51.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2024-06-01T11:48:52.000Z (6 months ago)
- Last Synced: 2024-11-29T08:43:04.889Z (14 days ago)
- Language: Python
- Homepage: https://pypi.org/project/flake8-builtins
- Size: 329 KB
- Stars: 114
- Watchers: 5
- Forks: 24
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-flake8-extensions - flake8-builtins - Check for python builtins being used as variables or parameters. (Naming)
- best-of-python-dev - GitHub - 4% open · ⏱️ 09.04.2024): (Linters & Style Checkers)
- awesome-python-backend - _flake8-builtins_ - checks for accidental use of builtin functions as names (Topics Index / Code Quality and Linting)
README
.. -*- coding: utf-8 -*-
.. image:: https://github.com/gforcada/flake8-builtins/actions/workflows/testing.yml/badge.svg?branch=main
:target: https://github.com/gforcada/flake8-builtins/actions/workflows/testing.yml.. image:: https://coveralls.io/repos/gforcada/flake8-builtins/badge.svg?branch=main
:target: https://coveralls.io/github/gforcada/flake8-builtins?branch=mainFlake8 Builtins plugin
======================
Check for python builtins being used as variables or parameters.Imagine some code like this:
.. code:: Python
def max_values(list, list2):
max = list[0]
for x in list:
if x > 0:
max = xall_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
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:.. code:: Python
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 builtinInstall
-------
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, 3.12, and pypy3
- flake8Rules
-----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.A005:
A module is shadowing a Python builtin module (e.g: `logging` or `socket`)A006:
A lambda argument is shadowing a Python builtin.License
-------
GPL 2.0