reportUnnecessaryIsInstance.md
April 8, 2026 · View on GitHub
Overview
reportUnnecessaryIsInstance is a diagnostic in Pylance and Pyright that warns when an isinstance check is unnecessary—such as when the type is already known or the check is redundant. This helps keep code clean and avoids misleading or unreachable code paths.
Representative Issues
- #2080: Warn or error on unreachable statements/expressions, providing clearer diagnostic messages.
- #3065: When using
isinstancein an exhaustive check where all possible types are covered byUnion, suppress the warning by adding anelseclause.
Examples
Error:
def greet(name: str) -> str:
if isinstance(name, str): # Always True — name is already str
return f"Hello, {name}"
return "Hello" # Unreachable
Fix — remove the redundant check:
def greet(name: str) -> str:
return f"Hello, {name}"
If the function genuinely accepts multiple types, widen the parameter annotation:
def greet(name: str | int) -> str:
if isinstance(name, str):
return f"Hello, {name}"
return f"Hello, #{name}"
Common Fixes & Workarounds
- Remove unnecessary
isinstancechecks when the type is already known or guaranteed. - Refactor code to avoid unreachable or redundant branches.
- Add an
elseclause when using exhaustive type checks withUniontypes. - Review the Pyright configuration documentation for options to adjust or suppress this diagnostic if needed.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default