`filter` does not narrow `TypeVar` used in `TypeGuard` signature
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Using filter with a TypeGuard, the TypeVar used in the TypeGuard signature is not properly narrowed.
To Reproduce
# tmp.py
from typing import Iterator, Optional, TypeVar
from typing_extensions import TypeGuard
T = TypeVar("T")
def is_not_none(x: Optional[T]) -> TypeGuard[T]:
return x is not None
maybe_ints: list[Optional[int]] = [1, None, 2]
ints: Iterator[int] = filter(is_not_none, maybe_ints)
$ mypy tmp.py
tmp.py:14: error: Argument 1 to "filter" has incompatible type "Callable[[Optional[T]], TypeGuard[T]]"; expected "Callable[[Optional[int]], TypeGuard[int]]"
Found 1 error in 1 file (checked 1 source file)
Expected Behavior
I wouldn't expect any error. Since the second argument to filter is typed as list[Optional[int]], mypy should narrow the TypeVar T appropriately.
Your Environment
- Mypy version used:
mypy 0.950 - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10
This may be related to https://github.com/python/mypy/issues/12682. I decided to open a new ticket instead of commenting on that issue because the repro above feels different from the one reported on that issue.
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 damit, den Bericht mit tmp.py und mypy 0.950 oder der aktuellen Version zu reproduzieren, wobei der Fokus auf filter mit is_not_none und dem TypeGuard TypeVar liegt. Verfolge den Type-Checking-Pfad für die Inferenz des filter-Callbacks und die TypeGuard-Eingrenzung; abgeschlossen ist die Aufgabe, wenn das Beispiel den Type-Check besteht und ints als Iterator[int] akzeptiert wird.
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
- 38/100