In Python, assert statement produces no code if command line optimization options -O or -OO are passed. Does it happen for typing.assert_never()? Is it safe to declare runtime assertions that will not be optimized out?
Consider the case
from typing import assert_never
def func(item: int | str):
match item:
case int():
...
case str():
...
case _:
assert_never(item)
Is it guaranteed that the default branch will work even in optimized mode?
>Solution :
No. assert_never() is just a normal function consisting of a raise statement, at least in CPython. It is not removed at runtime, as with other typing features.