4. Use @override for overridden methods
In Python 3.12 and later, use @override to mark methods that are intended to override a base-class method.
The decorator turns a design intention into something type checkers and reviewers can verify. That helps catch drift when a base-class method is renamed or a subclass method signature no longer matches.
Note
Python 3.12+
4.1. Don’t do this
1class BaseFormatter:
2 def format(self, value):
3 return str(value)
4
5class JsonFormatter(BaseFormatter):
6 # overrides BaseFormatter.format
7 def format(self, value):
8 return f'"{value}"'
4.2. Do this
1from typing import override
2
3class BaseFormatter:
4 def format(self, value):
5 return str(value)
6
7class JsonFormatter(BaseFormatter):
8 @override
9 def format(self, value):
10 return f'"{value}"'