python / python/mypy

(false positive) Using `@final` decorator on a `Protocol` class results in an error in most cases

Aperta
#17,288 3 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

Applying the @final decorator on a class that inherits from Protocol results in an error when the class implements abstract attributes (attributes, methods, properties). Protocols often implement abstract attributes or methods.


The original reasoning behind the error is that, for normal classes, the abstract attributes could never be implemented if the class is final.
But this reasoning doesn't really extend to Protocol since they support virtual subclasses.

There are at least a few potentially valid use cases for declaring a Protocol as final.
In my use case, a few of my concrete implementations are unable to subclass the Protocol because of issues with property overriding. Since this Protocol is very tiny and has no default implementations anyway, it makes sense to enforce that none of the concrete implementations subclass from it, for consistency and a bit more freedom, as in certain cases it isn't possible to and in my opinion there aren't many benefits.

To Reproduce

from typing import Protocol, final

@final
class Inter(Protocol):
    x: int
    
    def y(self) -> int:
        ...

    @property
    def z(self) -> int:
        ...

https://gist.github.com/mypy-play/95be80b2ef05cc43ed77677df133d9d5

Error
main.py:4: error: Final class __main__.Inter has abstract attributes "x", "y", "z" [misc]

mypy thinks that abstract implementations in a final class is useless, as per #8316.
For Protocols however, the virtual subclasses may implement the abstract attributes without needing to subclass the Protocol.

Your Environment

(Default mypy playground env)

  • Mypy version used: 1.10.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 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 il riproduttore Python fornito e confronta la diagnostica per un Protocol final con attributi astratti con il comportamento delle normali classi final. Il lavoro è completato quando un Protocol final con attributi astratti non produce l'errore di falso positivo segnalato, mentre il controllo esistente per le classi final continua a essere corretto.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.