Skip to content

prefer-required-constructor-parameters

Prefer required constructor parameters so callers choose dependencies and configuration explicitly.

Why

Defaulted constructor parameters hide choices at the composition boundary. Replacing None with an empty string, a constant, or a factory still permits an implicit choice. A required nullable parameter makes a meaningful absent value explicit without inventing a sentinel.

Fix

Require the parameter and update callers to pass their choice explicitly; preserve meaningful None values. Suppress intentional framework or public-library defaults with a compatibility reason.

Examples

Before — flagged Constructor silently selects an empty configuration
app/service.py
class Service:
def __init__(self, agent_name: str = "") -> None:
self.agent_name = agent_name
After — preferred Caller explicitly chooses an absent configuration
app/service.py
class Service:
def __init__(self, agent_name: str | None) -> None:
self.agent_name = agent_name

Formerly: discourage-nullable-constructor-parameters