Skip to content

Erronous 'Incompatible type' when using descriptor in metaclass #9758

Open
@noperator-zz

Description

@noperator-zz

Bug Report

When using descriptors in a metaclass, mypy will errounously throw 'Incompatible types in assignment' when accessing the described attribute in the derived class.

To Reproduce

` from typing import Generic, TypeVar, Any
from dataclasses import dataclass

@dataclass
class DUT:
    Description: str = ""
    ProductNumber: int = 0

T = TypeVar('T')

class DataDescriptor(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value

    def __set_name__(self, owner: Any, name: str) -> None:
        self.name = name

    def __get__(self, instance: Any, cls: Any) -> T:
        return self.value

    def __set__(self, obj: Any, value: T) -> None:
        self.value = value

class DataModelMeta(type):
    current_dut = DataDescriptor(DUT())

class DataModel(object, metaclass=DataModelMeta):
    pass


dut: DUT = DUT("hello", 1234)
reveal_type(DataModel.current_dut)
reveal_type(dut)
DataModel.current_dut = dut`

# main.py:32: note: Revealed type is 'main.DUT*'
# main.py:33: note: Revealed type is 'main.DUT'
# main.py:34: error: Incompatible types in assignment (expression has type "DUT", variable has type "DataDescriptor[DUT]")
# Found 1 error in 1 file (checked 1 source file)

Expected Behavior

Assignment to DataModel.current_dut should expect the DUT type. mypy was able to infer this, as shown by reveal_type(DataModel.current_dut).

Actual Behavior

mypy throws an 'Incompatible type' error at the assignment

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.6:db45529
  • Operating system and version: Windows 7

This can also be reproduced on the latest Python / mypy at https://mypy-play.net

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions