wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

Forbid chaining `is`

Open orsinium opened this issue 2 years ago • 3 comments

Rule request

Thesis

Forbid ast.Is in ast.Compare.ops when it's size is not zero. For example:

a = None
a is None           # True, as expected
a is None is True   # False 🤔
a is None == True   # False 🤔
a is None is None   # True 🤯 

Reasoning

From the AST perspective, is is an operator and can be chained. That can lead to unexpected results when the author wanted to compare the result of a is b operation. Instead, Python will chain the operations and compare the last argument of the previous operation.

orsinium avatar Jul 04 '22 07:07 orsinium

Also, I think the same applies to in

orsinium avatar Jul 04 '22 07:07 orsinium

Yes, totally.

sobolevn avatar Jul 04 '22 08:07 sobolevn

Hi, I'm new to contributing and would like to work on this issue.

JanaHafez1 avatar Jul 13 '22 11:07 JanaHafez1

If there is no progress on this issue, then I would like to implement it.

lensvol avatar Oct 01 '22 20:10 lensvol

Hi, @lensvol! Happy to see you :)

sobolevn avatar Oct 01 '22 21:10 sobolevn

@sobolevn You know me, can't stay away from a free T-shirt :)

lensvol avatar Oct 02 '22 07:10 lensvol

Implemented in https://github.com/wemake-services/wemake-python-styleguide/pull/2527, please review.

lensvol avatar Oct 10 '22 17:10 lensvol

If it is not resolved. I would like to work on this issue

adlizone avatar Jan 19 '23 18:01 adlizone

@adlizone feel free :)

sobolevn avatar Jan 19 '23 18:01 sobolevn

I think it is resolved in #2527, we just forgot to actualize the issue.

orsinium avatar Jan 20 '23 10:01 orsinium