python / python/mypy

Mypy doesn't object to `cached_property` methods that don't exist

Aperta
#18,327 2 commenti 1 reazione 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 includes functools.cached_property on it's list of property-list types, but considers it to be too identical to property. It allows the use of cached_property.setter and cached_property.deleter without raising an error, despite the fact that neither of these are methods that exist on cached_property.

I discovered this while looking at permutations of property-like behavior for https://github.com/python/typeshed/pull/13276.

Actually, I realized just now that it can be anything from any of the property types. See the second playground link. [Edit: this part fixed by #19313]

To Reproduce

Playground link showing : https://mypy-play.net/?mypy=latest&python=3.12&gist=f5336c7a8df8f028a565bab576b74062

This one shows that mypy doesn't object to non-existent methods of property used as a decorator:

https://mypy-play.net/?mypy=latest&python=3.12&gist=d91764177a604ba5640a1fe741718f52 [Edit: this part fixed by #19313]

Expected Behavior

I expect mypy to generate errors for methods that don't exist in the stubs, even with the special-cased behavior.

Actual Behavior

No errors are generated.

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 riproducendo il comportamento nel mypy playground collegato con Python 3.12. Poi esamina la gestione speciale di mypy per i tipi simili a property, concentrandoti sul motivo per cui cached_property accetta setter e deleter. Il lavoro è completato quando mypy segnala errori per i metodi assenti da cached_property, mantenendo al contempo il comportamento valido di cached_property.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.