Check that comparisons are overlapping [comparison-overlap]

If you use —strict-equality, mypy will generate an error if itthinks that a comparison operation is always true or false. These areoften bugs. Sometimes mypy is too picky and the comparison canactually be useful. Instead of disabling strict equality checkingeverywhere, you can use # type: ignore[comparison-overlap] toignore the issue on a particular line only.

Example:

  1. # mypy: strict-equality
  2.  
  3. def is_magic(x: bytes) -> bool:
  4. # Error: Non-overlapping equality check (left operand type: "bytes",
  5. # right operand type: "str") [comparison-overlap]
  6. return x == 'magic'

We can fix the error by changing the string literal to a bytesliteral:

  1. # mypy: strict-equality
  2.  
  3. def is_magic(x: bytes) -> bool:
  4. return x == b'magic' # OK