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 bool and Literal[0, 1] are explicitly converted to bool type 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

  1. Compare values of compatible types to avoid unnecessary or always-false comparisons.
  2. Refactor code to remove redundant or logically impossible comparisons.
  3. Use explicit type conversions where needed for clarity and correctness.
  4. Review the Pyright configuration documentation for options to adjust or suppress this diagnostic if needed.

See Also