Upgrading to Python 3.12: Essential Syntax and Performance Changes
Overview
Prerequisites
To get the most out of this guide, you should have a solid grasp of

Key Libraries & Tools
- pathlib: Updated with a new
walk()method for easier directory navigation. - typing: Updated to support new syntax for generics and the
@overridedecorator. - asyncio: Now the primary package for concurrent operations as older modules are retired.
Code Walkthrough
Simplified Generics
Previously, defining generic classes required manual TypeVar declarations.
# The Old Way
from typing import TypeVar, Generic
T = TypeVar("T", bound=str)
class Container(Generic[T]):
...
# The Python 3.12 Way
class Container[T: str]:
def get_data() -> T:
...
This new syntax eliminates the need for separate variable assignments and makes generic definitions look more like other modern languages.
The Override Decorator
To prevent accidental errors when subclassing, use the @override decorator to ensure you are actually modifying a base class method.
from typing import override
class Base:
def get_color(self):
return "blue"
class Child(Base):
@override
def get_color(self): # Type checkers will error if this is misspelled
return "red"
Syntax Notes
f"{strings["key"]}" directly within the template.
Practical Examples
Use the new pathlib.Path.walk() to recursively process files without relying on the older os.walk interface. This keeps your code consistent within the
Tips & Gotchas
Watch out for removed modules! distutils, asyncore, and asynchat. If your project still relies on these, migrate to