Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 24, 2024 11:08
Show Gist options
  • Save mypy-play/c0141ae83690f7cb8e2fb4cca120af32 to your computer and use it in GitHub Desktop.
Save mypy-play/c0141ae83690f7cb8e2fb4cca120af32 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import overload, Literal
class Foo:
@overload
def some_method(self, bar: str = '', *, flag: Literal[True]) -> int:
...
@overload
def some_method(self, bar: str = '', *, flag: Literal[False]) -> list[int]:
...
def some_method(self, bar: str = '', flag: bool = False) -> int | list[int]:
if flag:
return len(bar)
else:
return [len(bar)]
f = Foo()
f.some_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment