In Python 3.9 it is now possible to use collections.abc
with generics, meaning code such as this is possible:
import collections.abc
from typing import TypeVar
T = TypeVar("T")
class MySequence(collections.abc.Sequence[T]):
...
In Python 3.8, however, this is not possible. Instead, one must use the typing
module for generics. I'm wondering what the proper way to go about this is. Right now I'm doing the following:
import sys
from typing import TypeVar
T = TypeVar("T")
if sys.version_info < (3, 9):
import collections.abc, typing
class ABC_Sequence(collections.abc.Sequence, typing.Sequence[T]):
pass
else:
from collections.abc import Sequence as ABC_Sequence
class MySequence(ABC_Sequence[T]):
...
Is this the proper way to go about it?