Enhancing Decorators with Type Annotations: Techniques and Best Practices
summary
Shows concrete type-annotation patterns that make decorators verifiable: examples use ParamSpec, Protocol, Concatenate, TypeVarTuple and the new Python 3.12 type parameter syntax. Builds a logger-decorator that injects a remote-Logger into HTTP client callables, sends JSON logs to a server, and returns the original response; example code is uploaded. Demonstrates that mypy (older versions) misses some new syntax while pyright/pyre-style checkers catch misuse, and claims these hints find call-site errors before runtime.
AI-written summary · about
chapters (11)
- 0:00Decorators and type annotations
- 1:08Decorator typing challenges
- 3:00Python decorators basics
- 4:00Logging decorator for HTTP
- 7:34Typing protocols for interfaces
- 9:43ParamSpec for argument hints
- 11:31Concatenate for signatures
- 14:00Python 3.12 new types
- 15:50Enhanced logging decorator
- 18:29TypeVarTuple for variable-length argument handling
- 20:23Type checking tools
related talks
33mMetaprogramming in Python
24mA Taxonomy of Decorators: A-E
29mMetaprogramming with Decorators, Metaclasses, and Dynamic Code Generation
17mWrapping it up with Decorators
02h 21mIntroduction to Decorators: Power Up Your Python Code
18mExploring Function Annotations
12mType Annotations in Python: Terribly Intimidating or Tremendously Informative?
48mFalling in love with decorators ES6/TypeScript
