python / python/mypy

Property return type not inferred when using `field_name = property(func)`

Aperta
#13,745 0 commenti 2 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

When using the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)

To Reproduce

from __future__ import annotations
from typing import Any

def func1(self: Any) -> int:
    return 3

def func2(self: object) -> int:
    return 3

def func3(self: Test) -> int:
    return 3

class Test:
    def func4(self) -> int:
        return 3

    test1 = property(func1)
    test2 = property(func2)
    test3 = property(func3)
    test4 = property(func4)

reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)

# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file

Expected Behavior

I'd expect mypy to understand the property return types.

It also seems that there is no way around this.
The following gives "Incompatible types in assignment":

class Test:
    test4: int = property(func4)

and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)

Actual Behavior

We're getting Any instead.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.13

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

Il report non indica alcun file del repository né alcun test. Inizia eseguendo la riproduzione Python fornita con mypy 0.981 ed esamina la gestione di property/descriptor e la definizione di property in typeshed; il lavoro è completo quando reveal_type segnala il tipo restituito int di ogni property senza un'assegnazione incompatibile.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
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.