Skip to content
Sarj Code Standards

no-isinstance-type-error-guard

Declare the accepted type instead of raising TypeError after an isinstance check.

Why

A runtime type guard that raises TypeError means the signature admits values the code rejects; the contract is enforced by hand where type checkers cannot see it.

Fix

Narrow the parameter, attribute or return type so callers cannot pass the rejected value, parse untrusted input once at the boundary into a typed model, and end exhaustive type dispatch with assert_never.

Examples

Before — flagged A parameter accepts a type the function rejects
app/ports.py
def port(value: object) -> int:
if not isinstance(value, int):
raise TypeError(value)
return value
After — preferred The signature admits only the accepted type
app/ports.py
def port(value: int) -> int:
return value