Should bare logical comparisons raise a warning?

Hi everyone, I've been really impressed by the recent improvements to error messages in Python 3.10. The specific suggestions in some error messages are going to save countless hours of people's time. The benefits for beginners are fairly obvious. But even for experienced programmers, the difference between seeing a missing colon in an error message and just adding it where it should have been vs spending 30 seconds or a minute figuring out what's missing at the start of a loop adds up. One common error that I haven't seen discussed is bare logical comparisons. They're syntactically legal so they don't raise errors, but I have never seen a real-world use case for one. Here's the simplest example I can come up with: name = 'eric' print(name) name == 'ever' print(name) We want to assign a new value to an existing variable, but we accidentally type a second equals sign. The output here makes the mistake fairly obvious, but in longer programs it can take a while to spot this issue. I know there are some editors and linters that flag this. Is there any reason not to raise a warning or even an error at the language level? Is there any reason to use a bare logical comparison in real-world code?

On Thu, Nov 25, 2021 at 9:51 AM Barry <barry@barrys-emacs.org> wrote:
nor have I. However, this strikes me as very much a problem for linters to solve (and some do already). I just tried mine, and it doesn't.
name == 'ever'
I do get a undefined name error in the linter if I do this by itself, but, of course not if name haad been previously defined. However, even from a linter's perspective, I think this is not exactly misuse of "==", b ut rather a amore general "expression not assigned to anything" working. Of course, as Barry pointed out, you might have an unassigned expression in order to get a side effect. But the reason I think it's a linter's job is that I don't think there is any other case where Python tries to warn you about things that are not illegal. Yes, this is one example where it might make sense, but where do we draw the line? -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

On Wed, Nov 24, 2021 at 07:15:58PM -0000, ehmatthes@gmail.com wrote:
You've never used the interactive interpeter? *wink* You're right, of course, that outside of the REPL it would be very unusual, and quite rare, to use `==` for its side-effects. Another usual use would be to overload the `==` operator for a DSL. They might be rare, but they are allowed, and the Python interpreter doesn't generally raise warnings for legal expressions used as statement just because they *might* be a mistake. Some compilers and interpreters raise a plethora of warnings, or errors, for legal but "weird" code that might be a mistake. But Python's interpreter tends to be a lot more easy-going, assuming you know what you are doing, and leaves it up to third-party linters. IDEs and code checkers to be more opinionated. Personally, I think that this is the right design. People can pick and choose which, if any, linter they use, and how strict they want it to be. But I can also understand that some people might want the interpreter to also have a built-in linter to flag mistakes. -- Steve

On Fri, Nov 26, 2021 at 12:02 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
Agreed. Though there are a small number of cases where the language itself offers helpful warnings:
but these are reserved for situations that are very definitely, but more subtly, wrong (since this code will often appear to work just fine due to small-int caching). Chrisa

You've never used the interactive interpeter? *wink*
All of the responses here have been really helpful to read, but this line is the clearest. I'm almost certain I have used this syntax in the interpreter, but I spend so much time in editors I forgot about that use case. Thanks for the clarification, and thank you to everyone else in the thread as well. Eric

On Thu, Nov 25, 2021 at 9:51 AM Barry <barry@barrys-emacs.org> wrote:
nor have I. However, this strikes me as very much a problem for linters to solve (and some do already). I just tried mine, and it doesn't.
name == 'ever'
I do get a undefined name error in the linter if I do this by itself, but, of course not if name haad been previously defined. However, even from a linter's perspective, I think this is not exactly misuse of "==", b ut rather a amore general "expression not assigned to anything" working. Of course, as Barry pointed out, you might have an unassigned expression in order to get a side effect. But the reason I think it's a linter's job is that I don't think there is any other case where Python tries to warn you about things that are not illegal. Yes, this is one example where it might make sense, but where do we draw the line? -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

On Wed, Nov 24, 2021 at 07:15:58PM -0000, ehmatthes@gmail.com wrote:
You've never used the interactive interpeter? *wink* You're right, of course, that outside of the REPL it would be very unusual, and quite rare, to use `==` for its side-effects. Another usual use would be to overload the `==` operator for a DSL. They might be rare, but they are allowed, and the Python interpreter doesn't generally raise warnings for legal expressions used as statement just because they *might* be a mistake. Some compilers and interpreters raise a plethora of warnings, or errors, for legal but "weird" code that might be a mistake. But Python's interpreter tends to be a lot more easy-going, assuming you know what you are doing, and leaves it up to third-party linters. IDEs and code checkers to be more opinionated. Personally, I think that this is the right design. People can pick and choose which, if any, linter they use, and how strict they want it to be. But I can also understand that some people might want the interpreter to also have a built-in linter to flag mistakes. -- Steve

On Fri, Nov 26, 2021 at 12:02 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote:
Agreed. Though there are a small number of cases where the language itself offers helpful warnings:
but these are reserved for situations that are very definitely, but more subtly, wrong (since this code will often appear to work just fine due to small-int caching). Chrisa

You've never used the interactive interpeter? *wink*
All of the responses here have been really helpful to read, but this line is the clearest. I'm almost certain I have used this syntax in the interpreter, but I spend so much time in editors I forgot about that use case. Thanks for the clarification, and thank you to everyone else in the thread as well. Eric
participants (6)
-
Barry
-
Chris Angelico
-
Christopher Barker
-
ehmatthes@gmail.com
-
Rob Cliffe
-
Steven D'Aprano