Skip to content
Sarj Code Standards

prefer-explicit-class-contract

Declare a contract for a stateful behavior class.

Why

A class coordinating mutable state and multiple operations exposes a concrete implementation without declaring its substitution or inheritance contract.

Fix

Declare a focused shared ABC or Protocol for the execution behavior and type consumers against it. If the class is intentionally concrete with no substitution boundary, declare it final instead of inventing an unused abstraction.

Examples

Before — flagged The contract is implicit
app/service.py
from httpx import AsyncClient
class Execution:
def __init__(self, client: AsyncClient, state):
self.client = client
self.state = state
self.cancelled = False
def run(self):
return self.client.send(self.state)
def cancel(self):
self.state = None
After — preferred The contract is explicit
app/service.py
from httpx import AsyncClient
from typing import Protocol
class ExecutionPort(Protocol):
def run(self): ...
def cancel(self): ...
class Execution(ExecutionPort):
def __init__(self, client: AsyncClient, state):
self.client = client
self.state = state
self.cancelled = False
def run(self):
return self.client.send(self.state)
def cancel(self):
self.state = None