I’d like to do something like this. Let’s say I have a class X and want to make a class Y a specialization of UserList that may only contain instances of X. I’d like to get the most help from the static type checker as possible.
Here’s what I have so far:
class X:
pass
class Y(UserList[X]):
def __init__(self, initlist: list[X] = None):
self.data: list[X]
super().__init__(initlist)
But, I don’t know if this is sufficient without building a bunch of annoying test cases for the static type checker in my IDE. Is typing of a base class like this class Y(UserList[X]):
even a thing?
Do I ultimately need to override each of the methods of UserList, type the arguments and call super()
. I feel like I’m throwing things against the wall to see what sticks.
1
You can literally just do
class Y(UserList[X]): pass
You don’t even need the __init__
override.
If you don’t have any extra functionality to add (it’s not clear whether you do), you don’t even need Y
at all. You can just use UserList[X]
directly.