python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.48k stars 2.83k forks source link

Warn about comparing iterables for equality? #7264

Open JukkaL opened 5 years ago

JukkaL commented 5 years ago

Generally iter(x) == iter(x) is false, so comparing iterators and iterables for equality could often be a bug (or at least pointless). Maybe mypy should warn (at least optionally) about equality comparisons like these that have a high potential to be errors. Here is a list of things that seem suspect:

Mypy could recommend using the is operator instead.

I'm not sure if this is worth having. There may be some valid uses of comparing iterables using == instead of is, and it's not clear if this actually is a source of problems for programmers. A small experiment with some large codebase could help answer these questions.

I got this idea from a recent thread at python-dev.

ilevkivskyi commented 5 years ago

I think it makes sense to give an error for this if one uses --strict-equality, because the latter is essentially a warning about comparisons that are always false. The error message should be tweaked however (currently it mentions non-overlapping types).