Errors on @overload in supertype signatures
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Given ABC A which have two implementations B and C. Method C.view() may return either B or C, depending on argument type. When using @overload to describe this, mypy complains with "Signature of "view" incompatible with supertype "A"". However, the type annotations provided by the overload works as expected.
To Reproduce
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import overload, reveal_type
class A(ABC):
@abstractmethod
def view(self, x: int | None = None) -> A: ...
class B(A):
def view(self, x: int | None = None) -> B:
return B()
class C(A):
@overload # mypy complains: "Signature of "view" incompatible with supertype "A""
def view(self, x: int) -> B: ...
@overload
def view(self, x: None = None) -> C: ...
def view(self, x: int | None = None) -> B | C:
if x is None:
return C()
else:
return B()
# This works as expected when @overload is used
reveal_type(C().view(None)) # Revealed type is C
reveal_type(C().view(5)) # Revealed type is B
https://gist.github.com/mypy-play/00b068b5b6eeebbea6f9486dba468708
Actual Behavior
main.py:14: error: Signature of "view" incompatible with supertype "A" [override]
main.py:14: note: Superclass:
main.py:14: note: def view(self, x: int | None = ...) -> A
main.py:14: note: Subclass:
main.py:14: note: @overload
main.py:14: note: def view(self, x: int) -> B
main.py:14: note: @overload
main.py:14: note: def view(self, x: None = ...) -> C
Your Environment
- Mypy version used: 1.19.0
- Mypy command-line flags: Default args in gist
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: Python 3.12
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il riproduttore Python fornito con mypy 1.19.0 e analizza il controllo di compatibilità delle firme di overload e del supertipo. Il lavoro è completato quando l’esempio non segnala più un override incompatibile, mentre reveal_type continua a segnalare C per None e B per 5.
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
- 42/100