## 経緯
[[Python]]では[[Rust]]における[[パターンマッチ (Rust)|パターンマッチ]]のように、[[match文 (Python)|match文]]に分岐漏れがあっても実行時まで気づくことができない問題がある。
## 方法
ワイルドカード (`_`) で [[assert_never (Python)|assert_never]] を呼び出す。
```python
from typing import Literal, assert_never
type Animal = Literal["cat", "dog", "fish"]
def get_animal() -> Animal:
return "cat"
animal = get_animal()
match animal:
case "cat":
print("It's a cat!")
case "dog":
print("It's a dog!")
case "fish":
print("It's a fish!")
case _ as unreachable:
assert_never(animal)
```
網羅漏れがあると、[[assert_never (Python)|assert_never]] の引数内でエラーとなる。もちろん[[ランタイム]]時にもエラーとなる。
```python
match animal:
case "cat":
print("It's a cat!")
case "dog":
print("It's a dog!")
case _ as unreachable:
assert_never(animal)
#^^^^^^^
Argument of type "Literal['fish']" cannot be assigned to parameter "arg" of type "Never" in function "assert_never"
Type "Literal['fish']" is not assignable to type "Never" [reportArgumentType]
```
### 環境
| 対象 | バージョン |
| ---------- | ---------- |
| [[Python]] | 3.13.11 |