Python 3.12 and PEP 695 introduced type parameter lists [1], which can be bounded, e.g. restricted to subclasses of a specific class:
class BaseClass:
pass
class NewClass(BaseClass):
pass
def some_fun[T: BaseClass](somethings_type: type[T]):
pass
is valid in Python 3.12 from a Syntax point of view. However, running the same code in python3.9 results in a SyntaxError:
python3.9 test.py
File "/home/agocht/test/pep-965/test.py", line 9
def some_fun[T: BaseClass](somethings_type: type[T]):
^
SyntaxError: invalid syntax
Now I wonder: Is there a good way to use this feature while still writing backwards-compatible code?
Usually, I would fall back to typing-extensions, but since this is a syntax error and I didn't find anything related, I assume it's not that easy.
---
[1] https://docs.python.org/3/reference/compound_stmts.html#type-params