Overloads with Generics "does not accept all possible arguments"
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 45/100
Rechercherichtung
Beginne damit, das minimale Python-Beispiel aus dem Issue mit mypy auszuführen, und vergleiche sein Ergebnis mit dem verlinkten Pyright-Verhalten. Verfolge die Prüfung der Kompatibilität der Overload-Implementierung, die die Signaturen 1 und 2 meldet. Als abgeschlossen gilt die Aufgabe, wenn das Beispiel keine Kompatibilitätsfehler erzeugt und dabei die gezeigten assert_type-Ergebnisse erhalten bleiben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
I have a function whose return type is determined by the type of an input parameter (with a default value). I have attempted to type-hint it with generics. I get "error: Overloaded function implementation does not accept all possible arguments of signature".
To Reproduce
In the toy example below, the function has two return types (original-type or list) depending on two possible types of the force_list input (Force or NoForce). For completeness I have overloaded all 4 permutations of the input and output types.
Playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=283290e33e2618dfcdf447513d0f4543
from typing import TypeVar, assert_type, overload
class Force: ...
ForceInstance = Force()
class NoForce: ...
NoForceInstance = NoForce()
T = TypeVar("T")
@overload
def list_or_orig(x: list[T], force_list: Force = ...) -> list[T]: ...
@overload
def list_or_orig(x: list[T], force_list: NoForce = ...) -> list[T]: ...
@overload
def list_or_orig(x: T, force_list: Force = ...) -> list[T]: ...
@overload
def list_or_orig(x: T, force_list: NoForce = ...) -> T: ...
def list_or_orig(x: list[T] | T, force_list: Force | NoForce = ForceInstance) -> list[T] | T:
"""Return a list or scalar depending on force_list flag."""
if isinstance(x, list):
return x
elif isinstance(force_list, Force):
return [x]
else:
return x
a_list = list_or_orig(1) # Since default is Force, this will return list[int]
assert_type(a_list, list[int])
b_list = list_or_orig(1, ForceInstance)
assert_type(b_list, list[int])
a_int = list_or_orig(1, NoForceInstance) # This will return int
assert_type(a_int, int)
Expected Behavior
No errors when running through mypy.
Actual Behavior
main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 1 [misc]
main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
Found 2 errors in 1 file (checked 1 source file)
Notes
If I replace the generic type T with a specific type, say, int then it's fine.
I appreciate there is an ambiguity between list[T] and T but have included the less-general list[T] first in the overloads.
The same code passes in the pyright playground.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beitragsleitfaden
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/mypy
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
bug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100