Follow-up · depth 13
How would you test dataclasses and __slots__?
How would you test dataclasses and __slots__?
Answers use simple, clear English.
Quick interview answer
Test dataclasses and __slots__ with unit checks for the happy path, integration tests for real I/O boundaries, and load/chaos checks so regressions show up before prod. Core idea: Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
Detailed answer
Interview answer for testing dataclasses and __slots__: 1) Unit: isolate the pure logic behind dataclasses and __slots__ with deterministic fixtures. 2) Integration: exercise real timers/I/O/network boundaries the way production does. 3) Load & soak: prove the event path still meets SLOs under concurrency. 4) Failure injection: break dependencies and assert recovery/backpressure. Best practices to include: Use field(default_factory=list); kw_only=True for clarity in large models. Call out mistakes: Mutable default list in dataclass field; forgetting frozen for dict keys. Parent context: Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
Full explanation
Interviewers want a test strategy, not “I would write tests.” Tie each layer back to how dataclasses and __slots__ fails: correctness, latency, and starvation. @dataclass auto-generates __init__, __repr__, __eq__. slots=True (Py3.10+) fixes attributes and saves memory. frozen=True makes instances hashable if all fields hashable.
Follow-up questions
Only answered follow-ups are shown — click to open with full answers
Parent context — dataclasses and __slots__
Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
View full parent question →