Error when only assigning one of two typevars to concrete types (but no error when assigning two or zero)
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
When writing a generic class with two typevars, mypy accepts classmethods that (1) assign values based on both typevars and (2) assigns values based on no typevars; but does not accept a classmethod that assigns one value based on a typevar and one value based on a concrete value.
https://stackoverflow.com/q/74417503/1669576
To Reproduce
from typing import Generic, TypeVar
U = TypeVar('U')
V = TypeVar('V')
class Example(Generic[U, V]):
def __init__(self, a: U, b: V):
self._a = a
self._b = b
@classmethod
def first(cls, val: U):
return cls(val, None)
Expected Behavior
mypy should infer that None binds to V
Actual Behavior
mypy gives a seemingly spurious error error: Argument 2 to "Example" has incompatible type "None"; expected "V" [arg-type]
Your Environment
name: test
channels:
- conda-forge
- defaults
dependencies:
- _libgcc_mutex=0.1=conda_forge
- _openmp_mutex=4.5=2_kmp_llvm
- bzip2=1.0.8=h7b6447c_0
- ca-certificates=2022.9.24=ha878542_0
- certifi=2022.9.24=pyhd8ed1ab_0
- ld_impl_linux-64=2.38=h1181459_1
- libffi=3.3=he6710b0_2
- libgcc-ng=12.2.0=h65d4601_19
- libstdcxx-ng=11.2.0=h1234567_1
- libuuid=1.41.5=h5eee18b_0
- llvm-openmp=14.0.6=h9e868ea_0
- mypy=0.990=py310h5764c6d_2
- mypy_extensions=0.4.3=py310h06a4308_0
- ncurses=6.3=h5eee18b_3
- openssl=1.1.1s=h166bdaf_0
- pip=22.2.2=py310h06a4308_0
- psutil=5.9.0=py310h5eee18b_0
- python=3.10.6=haa1d7c7_1
- python_abi=3.10=2_cp310
- readline=8.2=h5eee18b_0
- setuptools=65.5.0=py310h06a4308_0
- sqlite=3.39.3=h5082296_0
- tk=8.6.12=h1ccaba5_0
- toml=0.10.2=pyhd3eb1b0_0
- tomli=2.0.1=pyhd8ed1ab_0
- typing_extensions=4.3.0=py310h06a4308_0
- tzdata=2022f=h04d1e81_0
- wheel=0.37.1=pyhd3eb1b0_0
- xz=5.2.6=h5eee18b_0
- zlib=1.2.13=h5eee18b_0
prefix: /home/b/miniconda3/envs/test
- Mypy version used: 0.990
- Mypy command-line flags: (none)
- Mypy configuration options from
mypy.ini(and other config files): (none) - Python version used: 3.10.6
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 la classe Example fornita con mypy 0.990 e senza flag di configurazione per riprodurre l'errore. Traccia quindi l'inferenza dei metodi di classe generici e delle variabili di tipo nel type checker, poi aggiungi un test di regressione che copra un argomento concreto e uno di variabile di tipo; il lavoro è completo quando mypy accetta l'esempio senza indebolire gli errori esistenti per zero o due variabili di tipo.
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
- 35/100