Function's argument type inference is affected by the functions return type when in a comprehension
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
MyPy seems to be getting confused if function that receives a generic class returns a union type with that generic is used in a comprehension. If the functions is used in a for loop or the function returns just the generic T, then MyPy doesn't show any errors.
To Reproduce
import dataclasses
from typing import Callable, TypeVar, Generic
T = TypeVar('T')
@dataclasses.dataclass(frozen=True, slots=True)
class Capsule(Generic[T]):
builder: Callable[[], T]
def process(argument: Capsule[T]) -> T | RuntimeError:
if isinstance((value := argument.builder()), int) and value % 2 != 0:
return RuntimeError('Not an even number')
return argument.builder()
encapsulated_ints = tuple(Capsule(builder=lambda: item) for item in range(10))
requests: list[int | RuntimeError] = []
for encapsulated_int in encapsulated_ints:
request = process(encapsulated_int)
requests.append(request)
requests = list(process(encapsulated_int) for encapsulated_int in encapsulated_ints)
Error:
main.py:25: error: Argument 1 to "process" has incompatible type "Capsule[int]"; expected "Capsule[int | RuntimeError]" [arg-type]
https://mypy-play.net/?mypy=latest&python=3.12&gist=ffe7822cc564a6f347c2b8d6fd98c47f
Expected Behavior
The list comprehension line should be interpreted exactly the same as the for loop.
Actual Behavior
MyPy somehow relates the function return to the generic type.
Your Environment
- Mypy version used: 1.9.0
- Mypy command-line flags: no flags
- Mypy configuration options from
mypy.ini(and other config files): no configs - Python version used: 3.12
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
Beginne mit dem Python-Reproduzierer im Issue und vergleiche die Diagnose für die for-Schleife mit der Generator-Comprehension. Verfolge, wie die Inferenz generischer Argumente in jedem Fall den Rückgabetyp der Funktion verwendet. Fertig ist es, wenn die Comprehension wie die Schleife akzeptiert wird und ein Regressionstest das gemeldete Beispiel abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100