You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when preserving generic type behaviour in subclasses (when not supported at runtime) #8523
The docs for "Using classes that are generic in stubs but not at runtime" (here) clearly indicate how to subclass classes that are defined to be generic in their stubs but don't actually support generics in their runtime code.
The shown method (conditionally defining a typed subclass) works great when you want to lock the type down in your subclass. However, if you want to maintain the subclass as a generic it does not work (mypy reports an error).
Here is failing case (with mypy --strict), where the only variation from the canonical example is using a generic TypeVar instead of a str as the type for the Queue:
fromqueueimportQueuefromtypingimportTYPE_CHECKING, TypeVar_T=TypeVar("_T")
ifTYPE_CHECKING:
MyQueueBase=Queue[_T] # change _T to str here and it works perfectly (per the docs)else:
MyQueueBase=QueueclassMyQueue(MyQueueBase): pass# mypy error is on this line (9)
mypy reports an error as follows (when the base is Queue[_T]):
$ python --version
Python 3.8.2
$ mypy --version
mypy 0.770
$ mypy --strict ./generic_subclass_example.py
generic_subclass_example.py:9: error: Missing type parameters for generic type "MyQueueBase"
Found 1 error in 1 file (checked 1 source file)
Note that without --strict there is no error (not sure which of the flags causes it).
How about this unfortunate hack (that creates another class in the hierarchy unfortunately)
from queue import Queue
from typing import TYPE_CHECKING, TypeVar, Generic
_T = TypeVar("_T")
if TYPE_CHECKING:
class MyQueueBase(Queue[_T]): pass
else:
class MyQueueBase(Generic[_T], Queue): pass
class MyQueue(MyQueueBase[_T]): pass
Activity
msullivan commentedon Mar 17, 2020
The relevant error flag is
--disallow-any-generics.I am actually not totally sure what the best way to handle this is?
msullivan commentedon Mar 17, 2020
How about this unfortunate hack (that creates another class in the hierarchy unfortunately)
Document PEP 585, 563, 604 and more.
Document PEP 585, 563, 604 and more (#9763)
Document PEP 585, 563, 604 and more (#9763)