Keep network calls out of result models
A small design rule for typed Python APIs.
Reading a track or playlist field should not unexpectedly issue a network request. A caller ought to be able to inspect and serialize a result without managing the network.
Separate the result from the client
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class Track:
name: str
uri: str
duration_ms: int | NoneThe HTTP client, not the model, handles requests and decoding. Use explicit methods for subsequent requests, and document whether a missing value is represented by None or an exception.
Types are not validation
Python annotations do not validate arbitrary JSON at runtime. Validate data at the decoding boundary, and test missing fields, unexpected types and changes to the upstream schema.