PyCQA / PyCQA/pyflakes

Warn on Unassigned `==` Expression as Statement

Open
#237 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
190
Avg merge
8m
Merged PRs (30d)
13

Description

Original report by scoutoss on Launchpad:


Working with some NumPy code recently, I accidentally wrote this:

<build output array>
<build mask array>

out[mask] == NaN  # This should have been a single `=`, not a ==.
return out

After a few minutes of debugging my NaN-less array, I realized that I had accidentally written == instead of =, turning my assignment statement into no-op comparison operator.

I'd be nice if PyFlakes gave a warning for a statement that's just an expression performing an equality comparison. There are very few cases where I'd expect that something like this is doing what the author intended.

A more aggressive version of this check would be to warn on any expression that's just a binary operator. The assumption there would be that the "statement" a + b is at best dead code, and at worst an operator with a highly non-obvious side-effect.

I'd be happy to work on either version of this if there's support for it.

Contributor guide

No contributing guide indexed for this repository

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 locating how PyFlakes handles standalone expression statements and where its warning tests are defined. Compare the requested equality-comparison warning with the broader binary-operator alternative in the issue, then determine a focused behavior and add coverage showing the intended warning and non-warning cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.