I have noticed the snippet posed here, which is:
class Iterable(Generic[T], extra=collections_abc.Iterable): # (*)
pass
class Iterator(Iterable, extra=collections_abc.Iterator):
pass
Question 1: How does extra=... work in this snippet? Or more generally, when can we put a keyword argument when subclassing? What does it do?
Question 2: As is mentioned in the discussion in the hub, the first class (marked with *) is a generic class parametrized on the type variable T. However, I am looking for something like this:
from typing import TypeVar, Generic
T = TypeVar('T')
class X(Generic[T]):
a = T
class Y(X[int]):
pass
Z = X[int]
print(Y.p) # gives "~T", but what I would like is "int"
print(Z.p) # also gives "~T"
In words, how to get a concrete subclass where the parameter T is fixed instead of the generic subclass with T undetermined? My first thoughts are something like these:
T = TypeVar(T)
class X(Generic[T]):
p = T
class F(X[T], arg, **kwargs): # as a functor with kwargs being the params?
# Do something to substitute T with arg
'''
This I suppose is incorrect and it is majorly
for this that I asked the first question.
'''
pass
# so that I can do
class Y(F, arg=P):
pass
I looked into type(name, bases, dict) reference about dynamical class creation (even though the situation can be static) but I failed to find any specification on the bases argument about putting a tuple (kwargs) in the args.