Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
How to get an abstract dataclass to pass mypy?

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.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Add # type: ignore to the decorator line. So in your case it would be:

from abc import ABC, abstractmethod
from dataclasses import dataclass

@dataclass  # type: ignore
class Liquid(ABC):

    @abstractmethod
    def drip(self) -> None:
        pass
over 4 years ago · Santiago Trujillo Denunciar

0

Create a dataclass as a mixin and let the ABC class inherit from it:

from abc import ABC, abstractmethod
from dataclasses import dataclass    

@dataclass
class LiquidDataclassMixin:
    my_var: str

class Liquid(ABC, LiquidDataclassMixin):
    @abstractmethod
    def drip(self) -> None:
        pass

This works with mypy type checking as well. I recommend against using # type: ignore as that defeats the point of type-checking. Taken from this GitHub issue.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda