typing.cast used
Detects usage of typing.cast() function calls, which can lead to runtime errors by bypassing type checking without any runtime validation.
typing.cast() tells the type checker to treat a value as a specific type without performing any runtime checks. Type checkers trust casts completely, so incorrect casts bypass all type safety guarantees.
What gets flagged
| Python | |
|---|---|
1 2 3 4 | |
Unsoundness Checker Output
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted
Better alternative
Instead of using cast(), use assert isinstance() to validate types at runtime:
| Python | |
|---|---|
1 2 3 4 5 6 | |
Unsoundness Checker: No Diagnostic Emitted
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted
This provides actual runtime safety instead of just lying to the type checker.
What is okay
If the type of the value you are casting is assignable to the target type, it is okay to use typing.cast().
| Python | |
|---|---|
1 2 3 4 | |
Unsoundness Checker: No Diagnostic Emitted
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted