Type narrowing issue: bounded TypeVar loses type information when list is sliced
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 slicing a list variable that is annotated with a bounded list TypeVar, mypy reveals the type of the sliced list as builtins.list[Any] instead of retaining the bounded type variable T. This results in a loss of type information.
To Reproduce
import typing
T = typing.TypeVar("T", bound=list)
def example(a: T):
reveal_type(a[:1]) # note: Revealed type is "builtins.list[Any]"
In the above code, I would expect the revealed type of a[:1] to be T, representing a sublist of the input list a. However, mypy instead reveals the type as builtins.list[Any], indicating a loss of the bounded type information.
Expected Behavior
I expected the revealed type to retain the bounded type variable T, which would maintain the type information of the elements in the list.
Actual Behavior
The revealed type is builtins.list[Any], demonstrating a loss of type information.
Your Environment
- Mypy version used: 1.5.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): not relevent i guess - Python version used: 3.9
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 den bereitgestellten Reproducer für Python 3.9 mit mypy 1.5.0 aus und verfolge, wie beschränkte TypeVars und das Slicing von Listen analysiert werden. Vergleiche den aufgedeckten Typ mit dem erwarteten T, füge anschließend Regressionstests für dieses Beispiel hinzu und überprüfe, dass die Typinformationen der Beschränkung erhalten bleiben.
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
- 35/100