Add support for simple lambda type guard
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Feature
Consider the following code:
from typing import Optional, List
list_with_none: List[Optional[int]] = [None, 1, 2]
filtered = filter(lambda v: v is not None, list_with_none)
reveal_type(filtered)
# Revealed type is 'typing.Iterator[Union[builtins.int, None]]'
It would be nice if mypy was able to parse simple lambda functions like this one, and infer that filtered is typing.Iterator[builtins.int].
Note that in this example, we use a simple none check, but this could work with more complicated checks:
list_of_tuples: List[Tuple[Optional[int], Optional[int]]]
filter(lambda v: v[0] is not None and v[1] is not None, list_of_tuples)
# would become `Iterator[Tuple[int, int]]`
Pitch
This is a fairly common idiom in Python.
Reasons why mypy might now want to do this:
- Supporting simple lambda is ok, but there will inevitably be a limit to what mypy can support
- We could use https://www.python.org/dev/peps/pep-0647/ to achieve the same thing without using lambdas
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 den beiden Filter-/Lambda-Beispielen und dem Verweis auf PEP 647; vergleiche die angeforderten aufgedeckten Typen und entscheide, welche einfachen Prädikate im Umfang liegen. Erledigt bedeutet, dass mypy für das None-Prüfbeispiel Iterator[int] und für die gepaarte Prüfung Iterator[Tuple[int, int]] ableitet, mit Abdeckung der unterstützten Fälle.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100