dataclass collection inference problem
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug Report
It seems that when typing a collection of dataclasses, if they all happen to have the same fields, then the inferred type will be a Callable instead of type.
The example below is self-contained and demonstrates the problem very clearly.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.14&gist=5dfadfda23e846913cbee4d56568b69e
from dataclasses import Field, dataclass
from typing import ClassVar, Protocol, TypeVar
@dataclass
class C1:
foo: int = 0
@dataclass
class C2:
foo: int = 1
@dataclass
class C3:
foo: int = 2
bar: str = ""
class Dataclass(Protocol):
__dataclass_fields__: ClassVar[dict[str, Field]]
DataclassT = TypeVar("DataclassT", bound=Dataclass)
def f(a1: list[type[DataclassT]]): ...
reveal_type([C1, C2])
reveal_type([C1, C3])
f([C1, C2]) # this gives an error, but the others work
f([C1, C3])
f([C2, C3])
f([C1, C2, C3])
Expected Behavior
I would expect dataclasses to always be typed as classes instead of becoming callables because they happen share fields.
Actual Behavior
t.py:31: note: Revealed type is "builtins.list[def (foo: builtins.int =) -> builtins.object]"
t.py:32: note: Revealed type is "builtins.list[builtins.type]"
t.py:34: error: Value of type variable "DataclassT" of "f" cannot be "object" [type-var]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.18.2 (but I tested 1.18.1, 1.17.1, 1.17.0 and they all have the same problem)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): follow_untyped_imports = true - Python version used: 3.14.0 (also fails with 3.13.*)
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza con el reproductor autocontenido del issue y el ejemplo enlazado de mypy-play; después, sigue la inferencia de tipos de mypy para los literales de lista que contienen clases dataclass. La tarea está terminada cuando [C1, C2] se infiere como una lista de tipos de clase en lugar de callables y todas las llamadas mostradas a f pasan la comprobación de tipos sin el error de type-variable.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100