mypy infer incorrect types in frozen dataclass with descriptors as attributes.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
I am having issues with frozen dataclass that inherits from a frozen dataclass with descriptors as attributes. mypy infer incorrect datatypes of the attributes from the base class. If the dataclasses are not frozen, then mypy infer correct datatypes in inherited class.
Below is the minimal code to reproduce the issue.
python=3.11
mypy=1.14.0
To Reproduce
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Self, SupportsFloat, TypeVar, overload, Type, Optional, Generic, Any
T = TypeVar("T")
V = TypeVar("V")
class Base(Generic[T, V], metaclass=ABCMeta):
def __set_name__(self, owner: type, name: str) -> None:
self.public_name = name
self.private_name = "_" + name
@overload
def __get__(self, instance: None, owner: Optional[Type[Any]] = None) -> Self:...
@overload
def __get__(self, instance: Any, owner: Optional[Type[Any]] = None) -> V | None:...
def __get__(self, instance: Any, owner: Optional[Type[Any]] = None) -> Self | V | None:
if instance is None:
return self
return getattr(instance, self.private_name, None)
def __set__(self, instance: Any, value: Optional[T]) -> None:
if value is self or value is None:
return
converted = self._converter(value)
object.__setattr__(instance, self.private_name, converted)
@abstractmethod
def _converter(self, value: T) -> V:
"""
Hook to implement conversion logic
"""
class FloatParameter(Base[SupportsFloat, float]):
def _converter(self, value: SupportsFloat) -> float:
"""Can have more logic here to check if value can be converted to float"""
return float(value)
@dataclass(frozen=True, kw_only=True)
class BaseDataClass:
float_param: FloatParameter = FloatParameter()
@dataclass(frozen=True, kw_only=True)
class DerivedDataClass(BaseDataClass):
boolean: bool = True
reveal_type(BaseDataClass.float_param) # correct FloatParameter
reveal_type(DerivedDataClass.float_param) # Incorrect Union[typing.SupportsFloat, None]
reveal_type(BaseDataClass().float_param) # Correct "Union[builtins.float, None]"
reveal_type(DerivedDataClass().float_param) # Incorrect Union[typing.SupportsFloat, None]
NOTE: Pylance on the other hand on VSCode shows correct datatypes.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione minima in Python 3.11 usando mypy 1.14.0 e confronta i risultati di reveal_type per BaseDataClass e DerivedDataClass. Analizza l’ereditarietà di dataclass congelate con attributi descriptor; il lavoro è completo quando la classe derivata segnala FloatParameter sulla classe e float | None sulle istanze, in corrispondenza della classe base.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 45/100