python / python/mypy

type: ignore for “incompatible with supertype” error in subclass should suffice for subsubclasses too

Aperta
#10,197 1 commento 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-inheritance topic-type-ignore
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

class A:
    def foo(self, s: str) -> str: ...

class B(A):
    def foo(self, n: float) -> float: ... # type: ignore

class C(B):
    def foo(self, n: float) -> float: ...

Mypy playground link

Mypy is correct to report an error for B.foo that necessitated a type: ignore comment. However, having added that type: ignore for B.foo, mypy should not also report an error for C.foo. Although C.foo is inconsistent with A.foo, it’s consistent with B.foo, and I have already asked mypy to ignore the inconsistency between B.foo and A.foo.

To put this another way, I recognize that mypy would fail to catch the runtime error in a: A = C(); a.foo("str"), but this would still be a runtime error if C.foo did not override B.foo, so the blame should be assigned solely to B.foo and not to C.foo.

This comes up in the typeshed stubs for markdown.inlinepatterns:

class Pattern:
    def handleMatch(self, m: Match) -> Optional[Union[str, Element]]: ...

class InlineProcessor(Pattern):
    def handleMatch(self, m: Match, data) -> Union[Tuple[Element, int, int], Tuple[None, None, None]]: ...  # type: ignore

These stubs correctly reflect the upstream API which uses handleMatch with a different signature in subclasses of InlineProcessor than in other subclasses of Pattern. That wasn’t a great API design decision, but now that it’s made and the type: ignore was added to InlineProcessor.handleMatch, ideally I shouldn’t have to repeat the same type: ignore in order to write a custom subclass of InlineProcessor whose handleMatch satisfies the signature of InlineProcessor.handleMatch.

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

Riproduci il comportamento di controllo delle sottoclassi nel mypy playground collegato, quindi confrontalo con gli stub di typeshed in stubs/Markdown/markdown/inlinepatterns.pyi e con l’implementazione upstream in markdown/treeprocessors.py. Il lavoro è completato quando un type: ignore su InlineProcessor.handleMatch impedisce anche un errore non necessario incompatible-with-supertype per un override compatibile di una sottoclasse, mentre la diagnostica originale di B.foo rimane soppressa.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.