python / python/mypy

Warn about comparing iterables for equality?

Open
#7,264 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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:

  • Comparing anything == Iterator.
  • Comparing anything == Iterable (since Iterator is Iterable).
  • Comparing dict.values() for equality (since d.values() != d.values()).

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed cases: equality comparisons with Iterator or Iterable types and dict.values(). The issue suggests a small experiment on a large codebase to measure whether these comparisons are common problems; use those findings to define the warning scope and whether recommending is is appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.