I'm looking for an easy way to pass a parameter to a subclass. https://www.python.org/dev/peps/pep-0487/ provides such a notation: class C(Base, arg=value). This works perfecly and typechecks correctly via mypy for a normal class, but it fails with a generic class inheriting some Generic[T] (see code below). What did I miss ?
I'm using Python 3.6.8 (stock Python3 of Ubuntu 18.04) and mypy 0.740.
Here are two minimal standalone examples: one can put them in two .py files, typecheck them and run them.
this code typechecks and runs fine:
# ok.py
# PEP 487, adapted from QuestBase example
class Foo(object):
variable = "???"
def __init_subclass__(cls, arg: str, **kwargs) -> None:
cls.variable = arg
super().__init_subclass__(**kwargs) # type: ignore
class Bar(Foo, arg="value"): ...
print(Bar.variable)
This code typechecks but fails at runtime with TypeError: __init_subclass__() missing 1 required positional argument: 'arg':
# problem.py
from typing import Generic, TypeVar
T = TypeVar('T')
# PEP 487, adapted from QuestBase example
class Foo(Generic[T]):
variable = "???"
def __init_subclass__(cls, arg: str, **kwargs) -> None:
cls.variable = arg
super().__init_subclass__(**kwargs) # type: ignore
class Bar(Foo[T], arg="value"): ... # crash
Crash log:
Traceback (most recent call last):
File "problem.py", line 12, in <module>
class Bar(Foo[T], arg="value"): ... # crash
File "/usr/lib/python3.6/typing.py", line 682, in inner
return func(*args, **kwds)
File "/usr/lib/python3.6/typing.py", line 1143, in __getitem__
orig_bases=self.__orig_bases__)
File "/usr/lib/python3.6/typing.py", line 978, in __new__
self = super().__new__(cls, name, bases, namespace, _root=True)
File "/usr/lib/python3.6/typing.py", line 137, in __new__
return super().__new__(cls, name, bases, namespace)
File "/usr/lib/python3.6/abc.py", line 133, in __new__
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
TypeError: __init_subclass__() missing 1 required positional argument: 'arg'
Obviously the crash in Python internal files reveals a problem in my file. Again, what did I miss ?
Is there a really typechecked solution or workaround ?
The following example is not a nice solution (wrong but typechecks):
# fake.py
from typing import Generic, TypeVar
T = TypeVar('T')
class Foo(Generic[T]):
variable = "???"
class Bar(Foo[T]):
variable = "value"
class KO(Foo[T]):
... # forgot assignment but still typechecks
print(KO.variable) # "???"
The following example, using a function which builds a class, is fine at runtime but doesn't typecheck: mypy doesn't recognize the result of a function as a derivable base class:
# param.py
from typing import Generic, TypeVar, Type
T = TypeVar('T')
class Foo(Generic[T]):
variable = "???"
def bar(arg: str) -> Type[Foo[T]]:
class C(Foo[T]):
variable = arg
return C
Bar: Type[Foo[float]] = bar("value")
print(Bar.variable)
class Baz(Bar): ... # doesn't typecheck
print(Baz.variable)
Error log:
param.py:16: error: Variable "param.Bar" is not valid as a type
param.py:16: error: Invalid base class "Bar"