Skip to content
Sarj Code Standards

no-hasattr

Do not probe attributes with the builtin hasattr.

Why

In strictly typed code, hasattr hides whether an attribute can be missing at all: it silences the type checker instead of fixing a loose type, so a renamed field quietly takes the fallback branch.

Fix

Narrow the value with isinstance, a Protocol, or a typed wrapper around the untyped library; keep a reasoned SARJ suppression only for genuine runtime feature detection.

Examples

Before — flagged A hasattr probe hides a loose type
service/agent.py
def model_name(settings: object) -> str:
return settings.model if hasattr(settings, "model") else "default"
After — preferred A typed parameter states which attributes exist
service/agent.py
from dataclasses import dataclass
@dataclass
class LLMSettings:
model: str
def model_name(settings: LLMSettings) -> str:
return settings.model