Skip to content

False positive violation when overriding attributes using descriptors #18695

Closed
@bartfeenstra

Description

@bartfeenstra

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions