python / python/mypy

Strange error when declaring `@final` property

Offen
#15,325 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Consider this code:

from typing import final

class A:
    def __init__(self):
        self._x = 4

    @property
    @final  # E: @final should be applied only to overload implementation
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value) -> None:
        self._x = value
        
        
class B(A):

    @property  # E: Cannot override final attribute "x" (previously declared in base class "A")
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value) -> None:
        self._x = value

https://mypy-play.net/?mypy=latest&python=3.11&gist=a4a1981a3db4ac546b9947bb344a5fd5

Mypy complains that @final is not applied to the "overload implementation", but the property is still successfully declared final.

I tried moving the @final decorator to the setter – in that case, mypy's first error goes away, but then also the second error vanishes, meaning that x wasn't actually marked as final.

(I also tried moving @final above @property but that produces the same result.)

Environment:
See mypy-play link above.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem reproduzierbaren Beispiel im Issue oder dem verlinkten mypy-play-Fall und vergleiche die Platzierung von @final am Property-Getter und -Setter. Verfolge, wie mypy final-Decorator validiert und Property-Attribute aufzeichnet, füge dann Abdeckung für diese Deklaration hinzu und überprüfe, dass die Diagnosen konsistent widerspiegeln, ob die Property final ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.