python / python/mypy

Descriptor access fails for complex generics

Aperta
#21,505 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando la gestione dell’accesso ai descrittori e la ricerca dei metodi speciali di Mypy, concentrandoti su come vengono valutate le chiamate associate e non associate a get. Usa il riproduttore e confronta entrambi i tipi rivelati con l’output previsto; il lavoro è completato quando Test().method produce il callable che restituisce Test senza un errore di tipo dell’argomento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.