Typing with ClassVar[Optional[Callable]] doesn't work if the callable has arguments
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
Typing an attribute in a base class with ClassVar[Optional[Callable]] results in a type error even though the typing is seemingly sound.
To Reproduce
Playground: https://mypy-play.net/?mypy=master&python=3.11&gist=4f52f561801eeb053b6d0a47c9afbc15
class BaseClass:
attr: ClassVar[Optional[Callable]] = None
class ChildClass2(BaseClass):
@staticmethod
def attr(s: str):
pass
Expected Behavior
Mypy shouldn't return any errors.
Additionally, the same should happen when Callable is also typed itself, e.g. Callable[[str], None]. Right now both fail when they shouldn't.
Actual Behavior
test.py:10: error: Signature of "attr" incompatible with supertype "BaseClass" [override]
Your Environment
- Mypy version used: 1.1.1, 1.2
- Mypy command-line flags: no relevant options
- Mypy configuration options from
mypy.ini(and other config files): no relevant options - Python version used: 3.11
Additional information
Interestingly, if the callable doesn't have any arguments, mypy doesn't fail:
class BaseClass:
attr: ClassVar[Optional[Callable]] = None
class ChildClass2(BaseClass):
@staticmethod
def attr():
pass
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 con la riproduzione collegata di mypy-play usando Python 3.11 e confronta i casi Callable non tipizzato e tipizzato. Segui il percorso di controllo degli override per un ClassVar Optional Callable e verifica che entrambi gli esempi di staticmethod non producano alcun errore di firma incompatibile.
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
- 35/100