Complete worked example
python
from dataclasses import dataclass, FrozenInstanceError
@dataclass(frozen=True, slots=True)
class Report:
labels: tuple[str, ...]
def inspect_example(labels):
record = Report(tuple(labels))
labels_copy = list(labels)
labels_copy.append('later')
try:
record.labels = ('changed',)
except FrozenInstanceError:
blocked = True
else:
blocked = False
return {'labels': list(record.labels), 'reassignmentBlocked': blocked,
'equal': record == Report(tuple(labels))}- Reasoning
- The worked program builds one Report, changes a separate list copy and then attempts an ordinary field reassignment. The FrozenInstanceError branch records that reassignment was blocked. Comparing with a second Report constructed from the same labels checks generated equality independently.