Created
December 12, 2023 08:37
-
-
Save mypy-play/1df3d1b4c9e82226df17094a9f9c6af5 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
import typing as t | |
def fails( | |
foo: int | None = None, | |
bar: int | None = None, | |
) -> None: | |
default = 1337 | |
match (foo, bar): | |
case (None, None): | |
foo = default | |
bar = default | |
case (None, val): | |
foo = val | |
case (val, None): | |
bar = val | |
case (_, _): | |
pass | |
case _: | |
# It would be great to be able to use `assert` here, but somehow | |
# mypy does not recognise that the match statement is exhaustive. | |
t.assert_never(foo) | |
def fails_too( | |
foo: int | None = None, | |
bar: int | None = None, | |
) -> None: | |
default = 1337 | |
match foo, bar: | |
case None, None: | |
foo = default | |
bar = default | |
case None, val: | |
foo = val | |
case val, None: | |
bar = val | |
case _, _: | |
pass | |
case _: | |
# It would be great to be able to use `assert` here, but somehow | |
# mypy does not recognise that the match statement is exhaustive. | |
t.assert_never(foo) | |
def fails_1tuple( | |
foo: int | None = None, | |
) -> None: | |
default = 1337 | |
match (foo,): | |
case (None,): | |
foo = default | |
case (_,): | |
pass | |
case _: | |
# It would be great to be able to use `assert` here, but somehow | |
# mypy does not recognise that the match statement is exhaustive. | |
t.assert_never(foo) | |
def works( | |
foo: int | None = None, | |
) -> None: | |
default = 1337 | |
match foo: | |
case None: | |
foo = default | |
case _val: | |
pass | |
case _: | |
t.assert_never(foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment