Skip to content

Wrong type inference for class with metaclass that acts as descriptor #10964

Open
@Jackenmen

Description

@Jackenmen

Bug Report
Descriptor protocol does not work on metaclasses - defining __get__ method in a metaclass does not make classes using that metaclass work as descriptors.

To Reproduce

  1. Put this code in a py file:
from typing import Any, TYPE_CHECKING, overload

class _IntDescriptorMeta(type):
    def __get__(self, instance: Any, owner: Any) -> int:
        return 123

class IntDescriptorClass(metaclass=_IntDescriptorMeta):
    ...

class IntDescriptor:
    def __get__(self, instance: Any, owner: Any) -> int:
        return 123

class X:
    number_cls = IntDescriptorClass
    number = IntDescriptor()

print(X.number_cls)
print(X().number_cls)
print(X.number)
print(X().number)

if TYPE_CHECKING:
    reveal_type(X.number_cls)
    reveal_type(X().number_cls)
    reveal_type(X.number)
    reveal_type(X().number)
  1. Run it, you should see:
123
123
123
123
  1. Type check it with mypy and see the incorrect output.

Expected Behavior
I expected mypy to infer type of X.number_cls/X().number_cls correctly:

main.py:24: note: Revealed type is "builtins.int"
main.py:25: note: Revealed type is "builtins.int"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"

Note:
To simplify this example, __get__ returns an int no matter if instance is None or not but I assume a fix would also make it work properly with overloads as it does for instances of classes.

Actual Behavior

Mypy does not infer type of X.number_cls/X().number_cls correctly:

main.py:24: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:25: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"

Your Environment

Additional notes
This works properly on pyright 1.1.161+, see the issue: microsoft/pyright#2164

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions