Skip to content

Python · architecture

no-hidden-constructor-fallback

python:no-hidden-constructor-fallback

Constructor option silently falls back to application settings when omitted.

Code
SARJ095
Default
error
Fix
none
Languages
python

Why

Hidden configuration lookup obscures dependencies and makes construction vary with ambient application state.

Fix

Require the constructor argument and resolve any default at the composition root or call site.

Before / after

Executed by this rule’s unit tests.

Before

Constructor reads an implicit default from settings

app/__init__.py

app/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
    MODEL: str = 'model'
settings = Settings()
app/service.py · focus
from app.config import settings

class Generator:
    def __init__(self, *, model: str | None = None) -> None:
        self.model = model or settings.MODEL

generator = Generator(model='explicit')
pyproject.toml
[project]
name = 'example'
version = '0.1.0'

After

Constructor requires its dependency

app/__init__.py

app/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
    MODEL: str = 'model'
settings = Settings()
app/service.py · focus
class Generator:
    def __init__(self, *, model: str) -> None:
        self.model = model

generator = Generator(model='explicit')
pyproject.toml
[project]
name = 'example'
version = '0.1.0'

Limits

  • Detection requires a proven local settings provider, a keyword-only optional parameter, and a first-party composition call.
  • Tests, generated files, migrations, descriptors, library environment fallbacks, and unconstructed classes are excluded.