Common type in container is determined using constructor args
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
To Reproduce
from typing import Type, List
class NonLiskovChild(Exception):
"""Does not match Exception.__init__() signature"""
def __init__(self, _some_other_arg: object) -> None:
super().__init__()
types1 = [TypeError, ValueError]
reveal_type(types1)
# Revealed type is 'builtins.list[def (*args: builtins.object) -> builtins.Exception]'
types2 = [TypeError, NonLiskovChild]
reveal_type(types2)
# Revealed type is 'builtins.list[builtins.type*]'
# When explicitly setting the type, mypy accepts NonLiskovChild as an Exception subtype:
types3: List[Type[Exception]] = [TypeError, NonLiskovChild]
reveal_type(types3)
# Revealed type is 'builtins.list[Type[builtins.Exception]]'
Expected Behavior
The lowest common type should be determined by inheritance, not with __init__() signature matching.
Otherwise stdlib classes like CalledProcessError do not work correctly, e.g. in this example:
import pytest
from subprocess import CalledProcessError
with pytest.raises((TypeError, CalledProcessError)) as exc_info:
pass
# error: Need type annotation for "exc_info"
# error: Argument 1 to "raises" has incompatible type "Tuple[Type[TypeError], Type[CalledProcessError]]"; expected "Union[Type[<nothing>], Tuple[Type[<nothing>], ...]]"
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.8.10
- Operating system and version: Windows 10
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 riproducendo i due esempi di reveal_type e il caso pytest.raises dell’issue. Traccia come viene inferito il tipo comune del contenitore a partire dai costruttori delle classi, quindi verifica che le sottoclassi di Exception indicate producano i tipi inferiti attesi e nessun errore dovuto ad argomenti incompatibili.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100