Complete worked example
python
from dataclasses import dataclass
@dataclass(frozen=True)
class Role:
id: str
title: str
def label(self):
return f"{self.id}: {self.title}"
def describe(raw):
if not isinstance(raw, dict):
raise ValueError("Expected a record")
values = [raw.get("id"), raw.get("title")]
if not all(isinstance(value, str) and value.strip() for value in values):
raise ValueError("Expected nonempty strings")
role = Role(values[0].strip(), values[1].strip())
return {"id": role.id, "label": role.label()}- Reasoning
- Runtime validation precedes construction; a separate probe checks ordinary frozen-field behavior.