Skip to content
Sarj Code Standards

no-dynamic-builtins

Use explicit typed state instead of dynamic Python builtin access.

Why

Dynamic namespace inspection, source compilation, and string-based attribute reads and writes hide dependencies from type checking and refactoring. The strict policy also rejects executor/debugger references that can otherwise escape their native call checks through an alias.

Fix

Pass typed state explicitly, use explicit imports and declared attributes, and keep source as data. A required compiler or plugin boundary needs a local, explained suppression.

Examples

Before — flagged A string field name hides the object contract
service/reader.py
def read(value, field):
return getattr(value, field, None)
After — preferred A declared field makes the contract explicit
service/reader.py
from dataclasses import dataclass
@dataclass(frozen=True)
class User:
name: str
def read(value: User) -> str:
return value.name
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

Formerly: no-hasattr