Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 14, 2025 11:35
Show Gist options
  • Save mypy-play/f5f9f5437041d07489d876a8d0b2da98 to your computer and use it in GitHub Desktop.
Save mypy-play/f5f9f5437041d07489d876a8d0b2da98 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
import typing as t
@t.overload
def stuff(isnone: t.Literal[True] = ...) -> None:
...
@t.overload
def stuff(isnone: t.Literal[False] = ...) -> str:
...
def stuff(isnone: bool = False) -> str | None:
if isnone:
return None
else:
return 'ᕕ( ᐛ )ᕗ'
a = stuff(True)
t.reveal_type(a) # expected None -> ok
b = stuff(False)
t.reveal_type(b) # expected builtins.str -> ok
c = stuff() # default is False
t.reveal_type(c) # expected builtins.str -> nope... got None v_v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment