python / python/mypy

@property returning descriptor instance triggers false positive

Aperta
#9,416 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🐛 Bug Report

Accessing a field implementing the descriptor protocol via a property method causes MyPy to complain that there is no matching overload for __get__().

To Reproduce

Run MyPy with all default settings on the following code:

from typing import Optional
from typing import overload
from typing import Type

class A:
    pass

class Field:
    def __init__(self, attr: Optional["Field"] = None):
        self.attr = attr

    @property
    def crashing_property(self) -> Optional["Field"]:
        return self.attr

    @overload
    def __get__(self, instance: None, owner: Type["A"]) -> "Field":
        ...

    @overload
    def __get__(self, instance: "A", owner: Type["A"]) -> Optional["Field"]:
        ...

    def __get__(self, instance, owner):
        if not instance:
            return self
        return self.attr

f = Field()  # type: Field
f.attr  # Works fine
f.crashing_property  # Triggers "no overload variant"

n.b. adding __set__() makes no difference

Expected Behavior

Accessing crashing_property shouldn't cause an error.

Actual Behavior

The last line in the example throws the following error:

test.py:39: error: No overload variant of "__get__" of "Field" matches argument types "Field", "Type[Field]"
test.py:39: note: Possible overload variants:
test.py:39: note:     def __get__(self, instance: None, owner: Type[A]) -> Field
test.py:39: note:     def __get__(self, instance: A, owner: Type[A]) -> Optional[Field]

If you add this the code validates fine:

@overload
def __get__(self, instance: "Field", owner: Type["Field"]):
    ...

Your Environment

  • Mypy version used: 0.782, 0.800, 0.812, 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None, using all defaults
  • Python version used: CPython 3.7.7, 3.8.6, 3.9.1
  • Operating system and version: Ubuntu 20.04, MacOS 11.1 (Big Sur)

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 eseguendo il riproduttore Python fornito con le impostazioni predefinite di MyPy e conferma l’errore di overload falso positivo nell’accesso alla proprietà. Il lavoro è completato quando tale accesso viene validato senza richiedere un overload get aggiuntivo per le istanze di Field, mantenendo valido il comportamento esistente del descrittore.

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à
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.