Skip to content

no-mutating-container-transform

Return container replacements without mutating borrowed builtin inputs.

Why

A container-valued transformation can change caller aliases before returning its result. Copying after the write leaves the original input changed.

Fix

Copy each changed container before writing, return a fresh replacement, or expose a separate command returning None. Keep documented in-place contracts explicit; do not discard the operation or add a dummy assignment.

Examples

Before — flagged Returning a list does not hide its mutation
app/counts.py
def ordered(values: list[int]) -> list[int]:
values.sort()
return values
After — preferred Build the replacement before changing it
app/counts.py
def ordered(values: list[int]) -> list[int]:
result = list(values)
result.sort()
return result