Warn about certain sequences of chained comparisons

Open
#690 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start with the linked TensorFlow example in tensorflow/python/eager/function.py and review Python's chained-comparison semantics. Compare the proposed operator-pair restrictions with pyflakes' existing checks, then define the warning behavior and coverage needed for intentionally chained comparisons and equivalent expressions.

Written by the indexing model from the issue text.

Description

I was pointed to this buggy line:

  if x is None != y is None:

(For context, Python allows chaining any of the comparison operators ==, !=, is, is not, >, >=, <, <=, in, not in. a OP b OP c is equivalent to a OP b and b OP c except that b is only evaluated once.)

I find it hard to imagine a use case for combining is and != in the same chained comparison, so it would be useful for linters to warn about this and similar patterns.

In my company's internal linter, I implemented a check that disallows all chained comparison pairs except == + ==, is + is, </<= + </<=, and >/>= + >/>=.

It found one false positive where someone had intentionally written == + !=, but I'd argue it's clearer with and:

-                        value.fullName == existing.fullName != "*"
+                        (value.fullName == existing.fullName and value.fullName != "*")

Would pyflakes be interested in a similar check?

Dominant language
Python
Stars
1.5k
Forks
207
Avg merge
4m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from PyCQA/pyflakes

All issues in PyCQA/pyflakes

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.