Created
September 6, 2023 06:49
-
-
Save mypy-play/41141b62a73edc56b6561088adea8ebf to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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