In field I have a function with more than 25 parameters returning tuple[int|None, int|None, int|None]
.
Only 3 bool
parameters change the return type (one for each element of the tuple) meaning I need 8 overloads overall and I don’t want to clutter the overloads with parameters that are irrelevant to the return type.
Code (neither mypy
nor pyright
complains):
from typing import Literal, overload
@overload
def test(switch: Literal[True]) -> int: ...
@overload
def test(switch: Literal[False]) -> None: ...
def test(switch: bool) -> int | None:
return 123 if switch is True else None
test_int: int = test(True)
test_none: None = test(False)
While this works:
def test(switch: bool, var: str = '') -> int | None:
I get errors with both those signatures regarding the overload implementation:
def test(switch: bool, var: str) -> int | None:
def test(var: str = '', switch: bool = True) -> int | None:
How can I change the signature of the overloads to signal to type checkers that only a single parameter is responsible for the return type, ignoring all the other parameters?