So, I was writing an event emitter class using Python.
Code currently looks like this:
from typing import Callable, Generic, ParamSpec
P = ParamSpec('P')
class Event(Generic[P]):
def __init__(self):
...
def addHandler(self, action : Callable[P, None]):
...
def removeHandler(self, action : Callable[P, None]):
...
def fire(self, *args : P.args, **kwargs : P.kwargs):
...
As you can see, annotations depend on ParamSpec
, which was added to typing
in python 3.10 only.
And while it works good in Python 3.10 (on my machine), it fails in Python 3.9 and older (on other machines) because ParamSpec
is a new feature.
So, how could I avoid importing ParamSpec
when running program or use some fallback alternative, while not confusing typing in editor (pyright)?
I don't know if there was any reason to reinvent the wheel, but
typing_extensions
module is maintained by python core team, supportspython3.7
and later and is used exactly for this purpose. You can just check python version and choose proper import source: