Wie tippe Überladungsfunktionen mit mehreren optionalen Argumenten ein?Python

Python-Programme
Anonymous
 Wie tippe Überladungsfunktionen mit mehreren optionalen Argumenten ein?

Post by Anonymous »

Ich habe eine Funktion mit mehreren KWARGs mit Standardeinstellungen. Einer von ihnen (in der Mitte irgendwo) ist ein boolescher Schalter, der den Rückgabetyp steuert.

Code: Select all

from typing import overload, Literal

@overload
def x(a: int = 5, t: Literal[True] = True, b: int = 5) -> int: ...

@overload
def x(a: int = 5, t: Literal[False] = False, b: int = 5) -> str: ...

def x(a: int = 5, t: bool = True, b: int = 5) -> int | str:
if t:
return 5
return "asd"
< /code>
Aber MyPy erhöht: < /p>

Fehler: Überlastete Funktionssignaturen 1 und 2 Überlappung mit nicht kompatiblen Rückgabetypen < /p>
< /blockquote>
Angenommen, das ist, dass X (). Falsch 
Wert in der zweiten Überladung, da Arg a mit einem Standard vorgegangen ist.

Code: Select all

x()
→ int
[*]

Code: Select all

x(t=True)
→ int
[*]

Code: Select all

x(t=False)
→ str

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post