Error when only assigning one of two typevars to concrete types (but no error when assigning two or zero)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führe zunächst die bereitgestellte Example-Klasse mit mypy 0.990 und ohne Konfigurations-Flags aus, um den Fehler zu reproduzieren. Verfolge anschließend die Inferenz von generischen Klassenmethoden und Typvariablen im Type Checker und füge einen Regressionstest hinzu, der ein konkretes sowie ein Typvariablen-Argument abdeckt; fertig ist die Aufgabe, wenn mypy das Beispiel akzeptiert, ohne die bestehenden Fehler für null oder zwei Typvariablen abzuschwächen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100