Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

False positive violation when overriding attributes using descriptors #18695

Open
bartfeenstra opened this issue Feb 17, 2025 · 0 comments
Open
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes

Comments

@bartfeenstra
Copy link

Bug Report

Overriding an attribute with a descriptor causes mypy to be unable to verify compliance with the parent's attribute.

This is similar to #17513, but at the moment it appears this is a distinct problem caused by child classes using descriptors.

To Reproduce

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import overload, TypeVar

_T = TypeVar("_T")


class Value:
    pass


class ContainerBase(ABC):
    @property
    @abstractmethod
    def value(self) -> Value:
        pass


class ValueAttr:
    @overload
    def __get__(self: _T, instance: None, owner: type[object]) -> _T:
        pass

    @overload
    def __get__(self, instance: _T, owner: type[_T]) -> Value:
        pass

    def __get__(self: _T, instance: object | None, owner: type[object]) -> Value | _T:
        if instance is None:
            return self
        return Value()


class Container(ContainerBase):
    value = ValueAttr()
# mypy says about the line above:
# error: Incompatible types in assignment (expression has type "ValueAttr", base class "ContainerBase" defined the type as
# "Value")  [assignment]

https://mypy-play.net/?mypy=latest&python=3.12&gist=bfbc9b2f6b26672a945ba5ab291a790b

Expected Behavior

No errors, and a Container instance's value attribute should be of type Value.

Actual Behavior

The error above, and a Container instance's value attribute type is inferred as Any.

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.0
@bartfeenstra bartfeenstra added the bug mypy got something wrong label Feb 17, 2025
@sterliakov sterliakov added the topic-descriptors Properties, class vs. instance attributes label Feb 17, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

2 participants