An interactive quiz tests understanding of Python 3.12's static typing improvements, including the new PEP 695 type variable syntax, differences between constrained and bounded type variables, the type statement for type aliases, the @override decorator, and typed dictionaries for annotating **kwargs.

2m read timeFrom realpython.com
Post cover image
Table of contents
Related ResourcesPython 3.12 Preview: Static Typing Improvements

Questions this post answers

How do I declare type variables using the new PEP 695 syntax in Python 3.12?

Python 3.12 introduces a new syntax for declaring type variables directly in generic class, function, and type alias definitions via PEP 695, removing the need to import TypeVar separately in many cases. This makes generics simpler to define. Constrained and bounded type variables behave differently: constrained type variables restrict a type to one of a specific set of types, while bounded type variables restrict it to a type and its subtypes. Developers adopting Python 3.12 typing syntax can track related changes and tutorials on daily.dev.

What does the @override decorator do in Python 3.12 typing?

The @override decorator lets developers explicitly mark a method as overriding a method in a parent class, helping model inheritance safely and catch refactoring mistakes when a base class method is renamed or removed. It works alongside typed dictionaries, which can annotate variable keyword arguments (**kwargs) precisely for stricter type checking. Teams refactoring class hierarchies in Python can follow typing updates like this on daily.dev.

122 Impressions