Skip to content

Descriptor access fails for complex generics #21505

@BenjyWiener

Description

@BenjyWiener

Bug Report

Mypy performs descriptor access by binding __get__ first and then evaluating the bound method for instance and owner. This causes problems when __get__ has an explicit self annotation that depends on the type of instance.

Mypy correctly infers the type when evaluating unbound __get__ and passing (descriptor, instance, owner) together.

It would be nice if Mypy could somehow make this work for any method (by delaying expansion of type variables in bound methods), but I understand that's a much bigger change than fixing for special method lookup, which doesn't actually do any binding at runtime.

To Reproduce

from collections.abc import Callable
from typing import overload, Concatenate, Self


class Descriptor[C: Callable]:
    def __init__(self, impl: C) -> None: ...

    @overload
    def __get__(
        self, instance: None, owner: type, /,
    ) -> Self: ...

    @overload
    def __get__[S, **P, R](
        self: Descriptor[Callable[Concatenate[S, P], R]],
        instance: S,
        owner: type[S] | None = None,
        /,
    ) -> Callable[P, R]: ...
    
    def __get__(self, *_) -> object:
        pass


class Test:
    @Descriptor
    # Explicit `self` type is necessary to prevent
    # Mypy from assuming this is a classmethod when
    # accessing via the class.
    def method[S: Test](self: S, foo: int, bar: str) -> S:
        return self


reveal_type(Test().method)
reveal_type(type(Test.method).__get__(Test.method, Test()))

(Playground)

Expected Behavior

main.py:34: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
main.py:35: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
Success: no issues found in 1 source file

Actual Behavior

main.py:34: error: Argument 1 to "__get__" of "Descriptor" has incompatible type "Test"; expected "None"  [arg-type]
main.py:34: note: Revealed type is "__main__.Descriptor[def [S <: __main__.Test] (self: S, foo: int, bar: str) -> S]"
main.py:35: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 2.1.0
  • Python version used: 3.12

Related Issues

#18036
#16554

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions