mypy v0.910 rejects abstract dataclasses in Python 3.9. Here's the Minimal Reproducible Example:
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class Liquid(ABC):
@abstractmethod
def drip(self) -> None:
pass
Here's the error message:
$ mypy --python-version 3.9 so.py
so.py:4: error: Only concrete class can be given where "Type[Liquid]" is expected
Found 1 error in 1 file (checked 1 source file)
How do I get this code to pass mypy?
Notes
I gather from mypy issue #5374 that this is a bug in mypy, first noticed in 2018 and still not corrected. I figure that people must be using mypy with abstract dataclasses, though, so there must be a workaround or a correct way to define or annotate the class. What is recommended?
The basis for the error message seems to be that mypy assumes that any object of type Type
can be instantiated, but abstract classes cannot be instantiated. This appears to be the error since Type
is defined to mean a class object, not necessarily a concrete class object (i.e. one that can be instantiated).
Adding # type: ignore
to the line containing class Liquid
does not block the error message. Since the code does not contain Type[Liquid]
, I figure it must be in the code generated by dataclass
. Type
is deprecated in Python 3.9, but apparently the dataclass
code generator still generates it.