polymorphic overloads on `list.__add__` and `dict.__or__` lead to divergences in type checkers.
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Beginne mit den Überladungen von list.add und dict.or, und prüfe dann PRs #14282 und #14284 auf die vorgeschlagene Vereinfachung. Reproduziere die list- und dict-Beispiele mit mypy und pyright und verwende mypy-primer, um zu verifizieren, dass die ausgewählten Stub-Änderungen die abweichenden Wrapper-Fehler entfernen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
The overloads on list.__add__ lead to divergent behavior between mypy and pyright when doing something as simple as a list concatenation
# Overloading looks unnecessary, but is needed to work around complex mypy problems
@overload
def __add__(self, value: list[_T], /) -> list[_T]: ...
@overload
def __add__(self, value: list[_S], /) -> list[_S | _T]: ...
Code sample in pyright playground, https://mypy-play.net/?mypy=latest&python=3.12&gist=abf6a8834020af17a16bd8cfb44b2f10
from typing import Any, overload
class ListA[T]: # emulates builtins list
@overload
def __add__(self, other: "ListA[T]", /) -> "ListA[T]": return ListA()
@overload
def __add__[S](self, other: "ListA[S]", /) -> "ListA[T | S]": return ListA()
class ListB[T]: # without overloads
def __add__[S](self, other: "ListB[S]", /) -> "ListB[T | S]": return ListB()
# mypy | pyright
reveal_type( list[str]() + list[str]() ) # list[str] | list[str] ✅️
reveal_type( list[str]() + list[int]() ) # list[str | int] | list[str | int] ✅️
reveal_type( list[str]() + list[Any]() ) # list[Any] | list[str] ❌️
reveal_type( ListA[str]() + ListA[str]() ) # ListA[str] | ListA[str] ✅️
reveal_type( ListA[str]() + ListA[int]() ) # ListA[str | int] | ListA[str | int] ✅️
reveal_type( ListA[str]() + ListA[Any]() ) # ListA[Any] | ListA[str] ❌️
reveal_type( ListB[str]() + ListB[str]() ) # ListB[str] | ListB[str] ✅️
reveal_type( ListB[str]() + ListB[int]() ) # ListB[str | int] | ListB[str | int] ✅️
reveal_type( ListB[str]() + ListB[Any]() ) # ListB[str | Any] | ListB[str | Any] ✅️
This ultimately causes some very annoying type errors when checking wrapper functions in pyright.
Code sample in pyright playground
from typing import Mapping, Any
# function with 2 optional arguments
def foo(arg: object, /, *, opt1: str = ..., opt2: int = ...) -> None: ...
# wrapper that forwards args via dict
def foo_wrapper(arg: object, foo_kwargs: Mapping[str, Any]) -> None:
# apply new defaults
foo_kwargs = {"opt1": "new_default"} | dict(foo_kwargs)
foo(arg, **foo_kwargs) # "str" cannot be assigned to parameter "opt2"
PR #14282 and #14284 show mypy-primer results of simplifying the overloads away from list.__add__ and dict.__or__.
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 2 T. 1 Std.
- Gemergte PRs (30 T.)
- 115
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typeshed
-
`networkx.set_edge_attributes` should support scalar value + nameEvtl. vergeben @Fire162 hat das vor 10 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 67/100
Maintainer antworten meist innerhalb von 1 Tag
-
false alarm on decode callEvtl. vergeben @jw9829 hat das vor 38 Tagen übernommen. Offenhelp wanted stubs: false positive
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/typeshed#16441 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Don't run pyright basic checks on projects with strict pyright settings in CIEvtl. vergeben @Mohith1612 hat das vor 74 Tagen übernommen. Offenproject: infrastructure
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
python/typeshed#16005 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
stubs: improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/typeshed#15548 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
stubs: improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
python/typeshed#15529 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in python/typeshed
Ähnliche Issues
-
Backend issue lists report in-patch warnings and out-of-patch errors as not publishableEvtl. vergeben @RaphaelFakhri hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
mozilla/code-review#3794 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[oblt-aw][autodoc] Update docs that still describe the security registry as emptyEvtl. vergeben @github-actions hat das heute übernommen. Offen
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] adb backup script reports "Done" & exits 0 when the backup failsEvtl. vergeben @bhattmanthan hat das heute übernommen. Offenrequest-issue-assignment
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
OWASP/mastg#4034 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
agent-ui bug p2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
amd/gaia#6580 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
correction metadata
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 72/100
acl-org/acl-anthology#10768 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag