Having fun with pydantic and pattern matching
summary
Asserts that match‑case (Python 3.10) is less necessary once messages are validated into typed models and routed, and builds an event consumer that reads endless JSON messages (Kafka/Rabbit style), validates them into pydantic models for account_created/account_updated/account_deleted, and dispatches handlers. Replaces many match cases with a pydantic TypeAdapter for Union models and then uses functools.singledispatch to register handlers. Malformed messages raise validation errors until union includes Any; solution works and is easy to extend though no performance benchmarks are shown.
AI-written summary · about
chapters (15)
- 0:00Introduction
- 1:04Python’s match-case statement
- 4:15Ordering in match-case patterns
- 5:39Event stream consumption case study
- 8:56Refactoring with match-case
- 14:22Untyped dictionaries problem
- 15:24Pydantic models for validation
- 17:34Pydantic with match-case
- 18:37Pydantic unions vs match-case
- 21:04Handling malformed data
- 22:09Specialized types for matching
- 23:13functools singledispatch
- 24:41Open-closed event handling
- 25:44Pattern matching power summary
- 27:51Q&A
related talks
26mIntro to Pydantic, run-time type checking for your dataclasses
28m"Validating Complex Types Using Pydantic"
10mAn Exploration of Python's Upcoming Match Statement
16mDynamic Pattern Matching with Python
21mLet JSON Schema and Pydantic write your data models
46mSupercharge your code with Python's 'match' statement
24mA Perfect Match The history, design, implementation, and future of Python's...
38mMigrating existing codebases to using type annotations
