Lenient TypeVarTuple checking when number of dimensions is unknown
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Currently this generates an error:
from typing import Any
class C[*Ts, S]:
pass
a = C[int, str]()
a = C[*tuple[Any, ...], str]() # Error
This is the output:
error: Incompatible types in assignment (expression has type "C[*tuple[Any, ...], str]", variable has type "C[int, str]")
However, this doesn't generate an error:
from typing import Any
class C[*Ts, S]:
pass
a = C[int, str]()
a = C[*tuple[Any, ...]]() # No error
I'd argue that the first example shouldn't generate an error either. One possible rule would be to allow this as long as some substitution of the *tuple[Any, ...] part would match the target type.
We could possibly also generalize the matching of unknown-length TypeVarTuple type arguments when the unknown-length part has a non-Any tuple item type. This could reduce apparent false positives. Example:
from typing import Any
class C[*Ts, S]:
pass
a = C[int, int]()
a = C[*tuple[int, ...]]() # No error?
This would only change the behavior of variadic generics. Variable-length tuples would continue to behave as they behave currently, i.e. tuple[int, ...] wouldn't be assignable to tuple[int, int]. The tuple behavior has been around for a very long time, so it doesn't make sense to change it, but variadic generics are a relatively new and untested feature.
The primary motivation is help with NumPy and libraries that provide multidimensional array-like types.
Proper subtype checks should continue to work as they work currently, since we can't safely simplify say C[int] | C[*tuple[Any, ...]].
cc @ilevkivskyi who I chatted about this recently
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 beiden TypeVarTuple-Beispiele aus dem Issue mit mypy aus und vergleiche ihre aktuellen Diagnosen. Untersuche die Pfade für die Kompatibilität von Variadic Generics und die Subtypprüfung und füge anschließend Abdeckung für die vorgeschlagenen Fälle mit unbekannter Länge für Any und non-Any hinzu, wobei das bestehende Verhalten von Tupeln variabler Länge erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100