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:
- # mypy: strict-equality
- def is_magic(x: bytes) -> bool:
- # Error: Non-overlapping equality check (left operand type: "bytes",
- # right operand type: "str") [comparison-overlap]
- return x == 'magic'
We can fix the error by changing the string literal to a bytesliteral:
- # mypy: strict-equality
- def is_magic(x: bytes) -> bool:
- return x == b'magic' # OK