Registry / testing / flake8-functions

flake8-functions

JSON →
library0.0.8pypypiunverified

flake8-functions is a Flake8 extension that checks for issues related to Python functions, such as excessive length, too many arguments, purity, and number of return statements. It provides specific error codes like CFQ001, CFQ002, CFQ003, and CFQ004. The current version is 0.0.8, released on April 10, 2023. This is an actively maintained project with releases as needed to address issues and enhance checks.

pip install flake8 flake8-functions
INSTALL
IMPORT
SIG · FLAKE8-FUNCTIONS
F
flake8-functions
testingpythonv0.0.8
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Install flake8 and flake8-functions, then run flake8 on a Python file. flake8-functions checks are automatically integrated. The example shows a function that exceeds a configured maximum length (CFQ001). To trigger this, a `max-function-length` is specified on the command line, or can be configured in a `.flake8` or `setup.cfg` file.

import os def some_long_function(first_parameter, second_parameter, third_parameter): # This function is intentionally long to trigger CFQ001 result = first_parameter + second_parameter result = result * third_parameter result = result / (first_parameter + 1) result = result % second_parameter result = result ** 2 result = result + first_parameter + second_parameter + third_parameter result = result * 5 - 10 result = result + 100 / (second_parameter - 1) result = result - (third_parameter * first_parameter) result = result * (first_parameter + second_parameter + third_parameter) return first_parameter def short_function(): return 1 # Save this code to a file named 'example.py' # Then run: flake8 --max-function-length=10 example.py
Debug
Known issues
gotchaFlake8 plugins are automatically activated upon installation. There are no direct Python imports required or exposed for `flake8-functions` within your code. Simply `pip install flake8-functions` and `flake8` will pick it up.
fix
Ensure `flake8-functions` is installed in the same environment as `flake8`. Run `flake8` command as usual; no extra configuration is needed for activation.
affects: All
gotchaThe default limits for checks (e.g., maximum function length, number of arguments, number of returns) might not align with every project's style guide. For instance, the default `max-function-length` is 100 lines.
fix
Configure `flake8-functions` settings in a `.flake8` or `setup.cfg` file in your project root, or via command-line flags. For example, to change max function length: `[flake8]
max-function-length = 50` or `flake8 --max-function-length=50`.
affects: All
gotchaThe definition of 'pure function' (CFQ003 check) implemented by `flake8-functions` might have specific interpretations or limitations, potentially leading to false positives or negatives depending on your project's context or Python version nuances. The documentation does not extensively detail its exact purity detection logic.
fix
Review the source code for the CFQ003 check if specific functions are unexpectedly flagged or missed. If the check is too strict or not suitable, it can be disabled using `--ignore=CFQ003` in your `flake8` configuration.
affects: All
gotchaWhile `flake8-functions` explicitly supports Python 3.7+ (and PyPI lists 3.6-3.10), `flake8` itself has evolving Python version requirements. Newer versions of `flake8` (e.g., `flake8` 7.2.0 requires Python >= 3.9) might implicitly make `flake8-functions` incompatible with older Python versions if your `flake8` dependency is not pinned.
fix
Always use a Python version compatible with both `flake8-functions` and your chosen version of `flake8`. It's recommended to use Python 3.9+ for modern `flake8` setups. Pin `flake8` and `flake8-functions` versions in your `requirements.txt` or `pyproject.toml` to ensure consistent behavior.
affects: All
Errors
Common errors & fixes
CFQ001 "some_function" function has length %d that exceeds max allowed length %d.
A function's line count (excluding blank lines, comments, and docstrings by default) exceeds the configured maximum length (default 100 lines).
fix
Refactor the function to be shorter, or increase the `max-function-length` setting in your Flake8 configuration (e.g., `setup.cfg` or `pyproject.toml`). To ignore a specific instance, add `# noqa: CFQ001` to the function definition line.
CFQ002 "some_function" function has %d arguments that exceeds max allowed %d.
A function is defined with more arguments than allowed by the `max-arguments-amount` configuration (default 6 arguments).
fix
Reduce the number of arguments (e.g., by passing an object or using `*args`/`**kwargs` carefully), or increase the `max-arguments-amount` setting in your Flake8 configuration. To ignore a specific instance, add `# noqa: CFQ002` to the function definition line.
CFQ003 "some_function" function is not pure.
The function performs side-effects, such as modifying global state, performing I/O, or calling non-pure functions, thus violating the purity principle.
fix
Refactor the function to avoid side-effects, making its output solely dependent on its inputs. If the side-effect is intentional and desired, ignore the error by adding `# noqa: CFQ003` to the function definition line.
CFQ004 "some_function" function has %d returns that exceeds max allowed %d.
A function contains more `return` statements than allowed by the `max-returns-amount` configuration (default 3 returns).
fix
Refactor the function to reduce the number of return paths (e.g., consolidate logic or use early exits more strategically), or increase the `max-returns-amount` setting in your Flake8 configuration. To ignore a specific instance, add `# noqa: CFQ004` to the function definition line.
Upgrade
Version history
0.0.8latest on PyPI · released Apr 10, 2023
Audit
Dependencies
flake8requiredThis library is a plugin for Flake8 and requires Flake8 to function.
PythonrequiredRequires Python 3.7 or newer. Although PyPI lists 3.6-3.10, the GitHub README specifies 3.7+.
Agent activity
2 hits · last 30 days
node
2
Resources
flake8-functions — pip install flake8-functions · libregistry