python / python/mypy

Descriptor access fails for complex generics

Open
#21,505 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Mypy’s descriptor access handling and special method lookup, focusing on how bound and unbound get calls are evaluated. Use the reproducer and compare both revealed types with the expected output; done means Test().method produces the callable returning Test without an argument-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.