typing.TypeIs used
Detects usage of typing.TypeIs in return type annotations, which can lead to runtime errors if the type narrowing function is implemented incorrectly.
typing.TypeIs is used for type narrowing functions that tell type checkers to narrow the type of a variable. Type checkers trust TypeIs functions completely, so incorrect implementations bypass all type safety guarantees.
What gets flagged
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Unsoundness Checker Output
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted
Better alternative
Instead of using TypeIs, use isinstance() checks directly in your code:
This provides actual runtime safety instead of just telling the type checker to trust your narrowing logic.
What is okay
If your TypeIs function correctly validates the type at runtime, it should be safe. However, since there's no way for type checkers to verify the implementation matches the signature, it's easy to introduce bugs.
| Python | |
|---|---|
1 2 3 4 | |
Unsoundness Checker Output
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted