Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about certain sequences of chained comparisons #690

Open
JelleZijlstra opened this issue May 4, 2022 · 1 comment
Open

Warn about certain sequences of chained comparisons #690

JelleZijlstra opened this issue May 4, 2022 · 1 comment

Comments

@JelleZijlstra
Copy link

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?

@asottile
Copy link
Member

asottile commented May 4, 2022

I think this probably makes more sense in flake8-bugbear (slightly more opinionated checks) but I definitely agree on making this an error! (it could go here as well, might want to check popular packages to see how many would get tripped by a new check)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants