reportUnnecessaryComparison.md
April 8, 2026 · View on GitHub
Overview
reportUnnecessaryComparison is a diagnostic in Pylance and Pyright that warns when a comparison is unnecessary or always evaluates to the same result due to type incompatibility or redundancy. This helps catch logic errors and keeps code clean and efficient.
Representative Issues
- #1744: Always ensure that the types being compared have a clear overlap or use type checking to handle comparisons, especially in conditional statements.
- #4861: Ensure that comparisons between
boolandLiteral[0, 1]are explicitly converted tobooltype to avoid false positives. - #5218: Ensure that collection membership checks are performed with compatible types to avoid unnecessary diagnostics.
Examples
Error:
x: str = "hello"
if x is None: # Condition always evaluates to False — str cannot be None
print("unreachable")
Fix — remove the unnecessary comparison:
x: str = "hello"
print(x) # No unnecessary check
Another common case — comparing incompatible types:
def find(items: list[int], target: str) -> bool:
return target in items # str can never equal int
Common Fixes & Workarounds
- Compare values of compatible types to avoid unnecessary or always-false comparisons.
- Refactor code to remove redundant or logically impossible comparisons.
- Use explicit type conversions where needed for clarity and correctness.
- 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