Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created December 12, 2023 08:37
Show Gist options
  • Save mypy-play/1df3d1b4c9e82226df17094a9f9c6af5 to your computer and use it in GitHub Desktop.
Save mypy-play/1df3d1b4c9e82226df17094a9f9c6af5 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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