DevPath
Senior lesson · 16 min

Design a stable Python library boundary

Separate parsing, domain rules, and public errors for long-lived callers.

Identify the public surface

A module used by several services needs a small documented API, stable input types, and explicit error behavior. Do not export every helper merely because it exists. Decide which values callers may construct directly and which require a factory that validates invariants.

Plan compatibility

Add optional behavior with safe defaults and deprecate old calls before removal. A type hint helps readers but does not validate runtime input. Normalize external dictionaries or strings at the boundary, then pass domain objects internally. Avoid changing exception types silently when callers use them for recovery.

def parse_booking(payload: Mapping[str, object]) -> Booking:
    # Validate every required field, then construct a trusted value.
    ...

Test consumer expectations

Write examples that represent real callers, including malformed input and a caller that catches a specific error. Version the contract when a breaking change is unavoidable. A migration note should show the old and new call, the behavioral difference, and how to detect incorrect use in production.

Check your understanding

Why is changing an exception type in a shared library potentially breaking?

Show explanation

Callers may catch that type to decide recovery behavior, even when the function signature is unchanged.

Try it at senior level

Split a parser library into input parsing, domain validation, and public errors. Evolve one field while keeping older callers working.

Check your work

The public API exposes stable error types, external input is validated at the edge, and compatibility tests cover the old call shape.

Self-guided practice. This does not record assessed mastery.

Put this lesson into practice

A related activity in this module at foundation level.

Try: De-duplicate values preserving order

Further reading

Primary references for this module. Check the current version when behavior or framework guidance may have changed.