python / python/mypy

mypy infer incorrect types in frozen dataclass with descriptors as attributes.

Open
#17,601 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

mypy-play.net

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Python 3.11 reproduction using mypy 1.14.0 and compare the reveal_type results for BaseDataClass and DerivedDataClass. Investigate frozen dataclass inheritance with descriptor attributes; done means the derived class reports FloatParameter on the class and float | None on instances, matching the base class.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.