Skip to content

python/function-too-large

Pack Severity Fix Versions
Python warning none, edit by hand All versions

Functions that grow past the fixed 50-statement limit accumulate copy-pasted branches and temporary variables that hide the data flow. Collapse repeated work into loops or data-driven mappings instead of moving statements into helpers.

def summarize(values):
# ... more than 50 repeated statements ...
return result
def summarize(values, specs):
result = {}
for name, fn in specs.items():
result[name] = fn(values)
return result

The rule counts executable statements inside each function body (including nested blocks and nested functions, excluding comments and docstrings) against a fixed 50-statement limit.

Existing oversized functions can be recorded at their current statement count alongside the module's total statements under [tool.xpd.ratchet] in pyproject.toml:

[tool.xpd.ratchet."src/pipeline.py"]
statements = 420
functions = { build_features = 120 }

Recorded ceilings may only shrink. Splitting a recorded function into helpers without shrinking the module's total statement count still fails the rule, and raising a ceiling relative to git HEAD fails the rule.

xpd has no automatic fix, so make the change by hand.

Delete repeated or unnecessary statements by collapsing parallel computations into loops or data-driven mappings, then lower or remove any [tool.xpd.ratchet] entry once the function shrinks.

This function exceeds the fixed 50-statement limit. Collapse repeated work into loops or data-driven mappings instead of moving statements into helpers.

See all rules in the Python pack.