Skip to content

duplicate-closed-pure-helper

Closed private helpers repeat one proven native regex/string transformation.

Why

Identical private transformations duplicate maintained behavior even when different caller patterns choose the domains in which they run.

Fix

Reuse one existing transformation while keeping each caller's pattern, order and examples. Preserve documented independent versioning or patch contracts explicitly.

Examples

Before — flagged Different patterns use identical closed callbacks
app/formatters.py
import re
FIRST = re.compile(r"item (\d+)")
SECOND = re.compile(r"code (\d+)")
def _space(match: re.Match[str]) -> str:
full = match.group(0)
digits = match.group(1)
spaced = " ".join(digits)
return full.replace(digits, spaced)
def _spread(match: re.Match[str]) -> str:
full = match.group(0)
digits = match.group(1)
spaced = " ".join(digits)
return full.replace(digits, spaced)
FIRST.sub(_space, text)
SECOND.sub(_spread, text)
After — preferred Patterns retain their choices and share one transformation
app/formatters.py
import re
FIRST = re.compile(r"item (\d+)")
SECOND = re.compile(r"code (\d+)")
def _space(match: re.Match[str]) -> str:
full = match.group(0)
digits = match.group(1)
spaced = " ".join(digits)
return full.replace(digits, spaced)
FIRST.sub(_space, text)
SECOND.sub(_space, text)