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
Warning for foo == "bar" or "baz" #92739
Comments
|
I think this would be great. |
|
I opened #92747 |
|
I could see this being useful. However, this need not be a problem if users are familiar with operator precedence rules or know where to look it up. |
|
I do not think this should be added in the compiler. It will complicate the code for very tiny benefit. Such kind of mistakes are only made by beginners which learn programming. It is easy to catch when it happen the first time, and it is unimaginative that anybody make it the second time. In contrary, the mistake in |
I have quite the opposite experience. I know a friend who was working on a non-trivial size program (hundreds of lines) when this happened to him for the first time. If I hadn't pointed it out in a review, the program would have behaved badly in a corner case that happens somewhat rarely. He didn't notice it himself when testing. |
|
Hm, I'm sort of on the fence about this. On one hand, it is an extremely common issue on StackOverflow. This question has 1,294 questions linked to/from it including many, many duplicates. I do buy the argument that the best tool to detect this is a linter, and needless complexity should be avoided, but I also think this might give a bit of benefit to beginners. I would defer to those with more experience teaching beginners though. I would assume that issue would be particular to those learning Python as their first programming language. |
IMHO The proposed warning can only find a very limited subset of possibly problematic patterns without resulting in a high level of false positives. The stack overflow question you link to is an example of that. That refers to "a or b or c == 2". In the context of that question it is clear that the OP should have used "a == 2 or b == 2 ...", but I've used similar code patterns where the code as written was correct. |
|
That particular heavily-linked-to post isn't of a form that could be caught at compile time, but browsing the top 15 "Hottest" of those 1294 links, most of these could safely be made to emit compiler warnings without bothering anyone:
|
|
There seems to be 3 different opinions about this:
For opinion 3:
I don't see how limited implies it's useless. Beginners use more hard-coded strings and ints in their if checks than experienced users, so restricting this to hard-coded values doesn't matter very much. The list above shows this quite clearly. This one boils down to whether we want "simple is better than complex" or "practicality beats purity". To me, a warning like this seems very practical, while others prefer keeping the compiler simple. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Feature or enhancement
Emit
SyntaxWarningfor code likeif foo == "bar" or "baz". It should be written asif foo == "bar" or foo == "baz", or perhapsif foo in ("bar", "baz"). The warning is emitted only if"bar"and"baz"here are string literals or number literals as inif foo == 1 or 2.5, butfoocan be any expression. Maybe also do the same forwhileandassert.Pitch
Currently there's no warning, the if statement just silently does the wrong thing. I have helped many people to learn programming in Python, and in my experience, this is a mistake that every beginner makes at some point during their learning.
In an
ifstatement, a condition like... or "some hard-coded string"always evaluates to a truthy value (unless the string is empty in which case theor ""can be safely removed). So in this context,or "some_string"can't really be intentional. It's an error and it shouldn't pass silently, but it does.Alternative ideas that I thought of and rejected:
oroperator raising runtime error if one of the operands is a string. Breaks correct code likereturn foobar or "some default value". Would also be ridiculously slow.CPython already emits a similar warning for
assert(condition, message), which asserts that a tuple of two elements is truthy.The text was updated successfully, but these errors were encountered: