Skip to content

Instantly share code, notes, and snippets.

@msakai
Created June 5, 2024 03:04
Show Gist options
  • Save msakai/86f9d8c0ee24918ea381e31923a4944c to your computer and use it in GitHub Desktop.
Save msakai/86f9d8c0ee24918ea381e31923a4944c to your computer and use it in GitHub Desktop.
from typing import Literal, Optional, Tuple, overload
@overload
def f(with_extra_info: Literal[False]) -> Tuple[int, int]: ...
@overload
def f(with_extra_info: Literal[True]) -> Tuple[int, int, str]: ...
# This overload is necessafy for type checking the last `f(with_extra_info)`.
@overload
def f(with_extra_info: bool) -> Tuple[int, int] | Tuple[int, int, str]: ...
def f(with_extra_info: bool) -> Tuple[int, int] | Tuple[int, int, str]:
if with_extra_info:
return (0, 1, 'extra info')
else:
return (0, 1)
with_extra_info: bool = True
extra_info: Optional[str]
if with_extra_info:
a, b, extra_info = f(with_extra_info)
else:
a, b = f(with_extra_info)
extra_info = None
f(with_extra_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment