Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created September 6, 2023 06:49
Show Gist options
  • Save mypy-play/41141b62a73edc56b6561088adea8ebf to your computer and use it in GitHub Desktop.
Save mypy-play/41141b62a73edc56b6561088adea8ebf to your computer and use it in GitHub Desktop.
Shared via mypy Playground
x: int | None = 1
# Either of these two type checks produces identical results:
if isinstance(x, int):
# if x is not None:
# Revealed type is "builtins.int"
reveal_type(x)
def f() -> int:
# Revealed type is "Union[builtins.int, None]"
reveal_type(x)
# error: Unsupported operand types for + ("None" and "int") [operator]
# note: Left operand is of type "int | None"
# return x + 2
# No errors here:
return 1 + 2
# error: Unsupported operand types for + ("None" and "int") [operator]
# note: Left operand is of type "int | None"
# (lambda: x + 2)()
# No errors here:
(lambda: 1 + 2)()
# However, defining a lambda without calling it does not cause errors:
l = lambda: x + 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment