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

Report useless ternary operator

Open orsinium opened this issue 4 years ago • 27 comments

Rule request

Thesis

Detect if both possible results of a ternary operator return the same value.

# bad
# (all results below can be replaced by just `a`)
a if ... else a
a if a is not None else None
a if a != b else b
b if a == b else a

# ok
a if ... else c
a.split() if a is not None else None
a if a != b else c

Reasoning

It is either a bug or unnecessary complexity.

orsinium avatar Oct 29 '20 10:10 orsinium

Hello word

locko62 avatar Nov 09 '20 07:11 locko62

@locko62 does it mean that you want to wrok on this issue? 🙂

sobolevn avatar Nov 09 '20 08:11 sobolevn

Hello, I would like to work on this? This is my first-time!

Praveenk8051 avatar Nov 09 '20 13:11 Praveenk8051

@Praveenk8051 you are welcome! Please, take a look at https://github.com/wemake-services/wemake-python-styleguide/blob/master/CONTRIBUTING.md

sobolevn avatar Nov 09 '20 14:11 sobolevn

Thank you!

Praveenk8051 avatar Nov 09 '20 14:11 Praveenk8051

Hi, I need some help. I went through documentation to know how to contribute and API References

I'm facing below error when I'm executing make test. Can you please help me with it ? @sobolevn wemake

I don't think i can just go ahead and install. Thanks!

Praveenk8051 avatar Nov 10 '20 11:11 Praveenk8051

I can recommend to remove your venv, make sure that your base python does not have any installed packages that might be causing this problem (check with pip freeze, it should be empty), and call poetry install once again after cleaning everything.

sobolevn avatar Nov 10 '20 11:11 sobolevn

Worked. Thank you!

Praveenk8051 avatar Nov 10 '20 12:11 Praveenk8051

Hello, I'm trying to debug and apply changes so that linter is applicable properly for ternary properties. But I'm failing to understand how to proceed. Can you please help me with it ? @sobolevn

This happens to be present in compares.py where NestedTernaryViolation is checked. Am I on right path ? Should i check and tweak in compares.py

Praveenk8051 avatar Nov 11 '20 11:11 Praveenk8051

Yes, please fix all the violations in our own source code found by a new rule!

sobolevn avatar Nov 11 '20 12:11 sobolevn

Hello @sobolevn. I would also like to work on this issue apart from #1974 with @bhargavi247. We are students who are working on an assignment to fix Issues for open source projects. Do let us know if this issue is still open! Thank you!

amansr02 avatar Apr 07 '21 01:04 amansr02

Yes, it is! Thanks!

sobolevn avatar Apr 07 '21 08:04 sobolevn

Awesome! Let us get started on this issue as well. If you could assign it to @bhargavi247 that would be great.

amansr02 avatar Apr 07 '21 19:04 amansr02

Thanks @sobolevn ! Would love to get assigned to this task :)

bhargavi247 avatar Apr 07 '21 23:04 bhargavi247

I would like to work on this issue, I am a first-time contributor Can someone guide me on how to proceed with this issue?

harshakhmk avatar Jun 04 '21 04:06 harshakhmk

Hi @harshakhmk! Please, take a look at https://github.com/wemake-services/wemake-python-styleguide/blob/master/CONTRIBUTING.md

sobolevn avatar Jun 04 '21 08:06 sobolevn

Hi! I'm fairly new to open source contribution but would like to give this issue a try. If this issue is unclaimed, could I possibly be assigned. Also, if I do need assistance, would I be able to receive guidance from other contributors in this repository?

mketer1 avatar Mar 22 '22 08:03 mketer1

This is hot....does anyone have the idea how to write a visitor for this

adlizone avatar Jan 21 '23 14:01 adlizone

#2579

adlizone avatar Feb 07 '23 16:02 adlizone

Is this issue still open? If so I would be interested in being assigned to this issue. Like many others here also new to open source and would appreciate the opportunity to learn.

FieldMarshallObvious avatar Mar 08 '24 17:03 FieldMarshallObvious

@FieldMarshallObvious please, go ahead :)

sobolevn avatar Mar 08 '24 19:03 sobolevn

you can take a look at https://github.com/wemake-services/wemake-python-styleguide/pull/2579 it was a promising start, but never fixed tests :(

sobolevn avatar Mar 08 '24 19:03 sobolevn

Thanks! Will do 👍

FieldMarshallObvious avatar Mar 08 '24 23:03 FieldMarshallObvious

Attempt to fix this on PR #2902

FieldMarshallObvious avatar Mar 28 '24 00:03 FieldMarshallObvious

@FieldMarshallObvious is working on it right now, please take some other issue :) There are multiple ones that are free!

sobolevn avatar Apr 01 '24 11:04 sobolevn

@sobolevn Can you please suggest other issues for beginners?

CodeCraftsman481 avatar Apr 01 '24 11:04 CodeCraftsman481