Specialize empty collections to Any as a fallback
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Context
I have a pending PR for typeshed which fixes overparameterized dict.get and dict.pop methods: python/typeshed#10294. The PR causes a lot of failures in various GitHub projects due to how mypy treats empty collections. Specifically, if dict.get is annotated as
class dict(Generic[KT, VT]):
def get(self, key: KT, default: T) -> T | VT: ...
mypy infers the type of v in
def test(d: dict[str, Any]):
v = d.get("foo", {})
to be Any | dict[<unknown>, <unknown>], instead of Any | dict[Any, Any].
Feature
Specialize empty collections to Any if no other information is available to recover type arguments.
Pitch
Using <unknown> instead of Any requires users to cast or add type annotations where they otherwise will not be necessary. See comments in the PR for a few examples from real projects.
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, das gezeigte dict.get-Beispiel in mypy zu reproduzieren und zu untersuchen, wie Ausdrücke für leere Collections inferiert werden, wenn keine Typargumente rekonstruiert werden können. Die Aufgabe ist abgeschlossen, wenn leere Collections in dieser Situation zu Any statt zu unknown spezialisiert werden und das Verhalten weiterhin durch relevante Type-Checking-Tests abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100