typing.TypeGuard
https://docs.python.org/ja/3/library/typing.html#typing.TypeGuard
#Python_typing
PEP 647 – User-Defined Type Guards
In short, the form def foo(arg: TypeA) -> TypeGuard[TypeB]: ..., means that if foo(arg) returns True, then arg narrows from TypeA to TypeB.
Using -> TypeGuard tells the static type checker that for a given function:
The return value is a boolean.
If the return value is True, the type of its argument is the type inside TypeGuard.
typing.TypeIsとの違い
TypeIs requires the narrowed type to be a subtype of the input type, while TypeGuard does not.
「TypeIsは狭められた型(入力した型のサブタイプ)を必要とするが、TypeGuardは必要としない」
ぱっと理解するのにオススメ
Python 3.10の新機能(その7) ユーザ定義型ガード
PEP647 TypeGuardの簡単な紹介
TODO:typeguardとの関係は?