python / python/mypy

Using `x = property(get_x)` in a class definition misbehaves

Aperta
#16,827 3 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
Merge medio
1g 18h
PR unite (30g)
54

Descrizione

[It's difficult to believe this hasn't been reported before, but, with apologies, I haven't been able to find an open issue describing this.]

Bug Report

Mypy doesn't support using @property as a function in a class definition. This makes it challenging to work around #6700.

We might expect these declarations to be similar (a11y version follows):

# with '@property' used as a function:            │  # with '@property' used as a decorator:
                                                  │
class SomeClass:                                  │  class SomeClass:
                                                  │
    def _get_foo(self) -> int:                    │      def _get_foo(self) -> int:
        return 42                                 │          return 42
                                                  │
    foo = property(get_foo)                       │      @property
                                                  │      def foo(self) -> int:
                                                  │          return _get_foo()
Accessible version of the above
# with '@property' used as a function:

class SomeClass:

    def _get_foo(self) -> int:
        return 42

    foo = property(get_foo)


# with '@property' used as a decorator:

class SomeClass:

    def _get_foo(self) -> int:
        return 42

    @property
    def foo(self) -> int:
        return _get_foo()

But in mypy, the left form results in foo having type Any, whether accessed on the class itself or an instance.

To Reproduce

mypy-play.net: gist

from typing_extensions import (
    assert_type,
    )

class SomeClass:
    @property
    def controlcase(self) -> int:
        return 42

    def get_testcase(self) -> int:
        return 42

    testcase = property(get_testcase)

inst = SomeClass()

# 'testcase' should behave the same way as 'controlcase':
reveal_type(SomeClass.controlcase)  # ... "def (self: SomeClass) -> int"
reveal_type(inst.controlcase)       # ... "int"

# but it does not:
reveal_type(SomeClass.testcase)     # ... "Any"
reveal_type(inst.testcase)          # ... "Any"

Expected Behavior

controlcase and testcase should be indistinguishable

Actual Behavior

Mypy doesn't understand @property when used as a function.

Your Environment

  • Mypy version used: 1.8.0 (and probably at least as far as 0.730)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8, 3.12

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 con la riproduzione su mypy-play.net e confronta il modo in cui vengono analizzate la forma con decoratore e la forma property(get_testcase). Usa i controlli reveal_type come criteri di completamento: SomeClass.testcase e inst.testcase dovrebbero corrispondere ai tipi controlcase corrispondenti invece di diventare Any.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.