Mike Graham | 8 Oct 21:44 2012
Picon

Make "is" checks on non-singleton literals errors

I regularly see learners using "is" to check for string equality and
sometimes other equality. Due to optimizations, they often come away
thinking it worked for them.

There are no cases where

    if x is "foo":

or

   if x is 4:

is actually the code someone intended to write.

Although this has no benefit to anyone but new learners, it also
doesn't really do any harm.

Mike
Guido van Rossum | 8 Oct 21:48 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
> I regularly see learners using "is" to check for string equality and
> sometimes other equality. Due to optimizations, they often come away
> thinking it worked for them.
>
> There are no cases where
>
>     if x is "foo":
>
> or
>
>    if x is 4:
>
> is actually the code someone intended to write.
>
> Although this has no benefit to anyone but new learners, it also
> doesn't really do any harm.

I think the best we can do is to make these SyntaxWarnings. I had the
same thought recently and I do agree that these are common beginners
mistakes that can easily hide bugs by succeeding in simple tests.

--

-- 
--Guido van Rossum (python.org/~guido)
Masklinn | 8 Oct 21:59 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 2012-10-08, at 21:48 , Guido van Rossum wrote:
> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
>> I regularly see learners using "is" to check for string equality and
>> sometimes other equality. Due to optimizations, they often come away
>> thinking it worked for them.
>> 
>> There are no cases where
>> 
>>    if x is "foo":
>> 
>> or
>> 
>>   if x is 4:
>> 
>> is actually the code someone intended to write.
>> 
>> Although this has no benefit to anyone but new learners, it also
>> doesn't really do any harm.
> 
> I think the best we can do is to make these SyntaxWarnings. I had the
> same thought recently and I do agree that these are common beginners
> mistakes that can easily hide bugs by succeeding in simple tests.

How would the rather common pattern of using an `object` instance as a
placeholder be handled? An identity test precisely expresses what is
meant and desired in that case, while an equality test does not.

An other one which seems to have some serious usage in the stdlib is
type-testing (e.g. collections.abc, decimal or tests of exception types).
Without type inference, I'm not too sure how that could be handled
(Continue reading)

Guido van Rossum | 8 Oct 22:07 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 12:59 PM, Masklinn <masklinn@...> wrote:
> On 2012-10-08, at 21:48 , Guido van Rossum wrote:
>> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
>>> I regularly see learners using "is" to check for string equality and
>>> sometimes other equality. Due to optimizations, they often come away
>>> thinking it worked for them.
>>>
>>> There are no cases where
>>>
>>>    if x is "foo":
>>>
>>> or
>>>
>>>   if x is 4:
>>>
>>> is actually the code someone intended to write.
>>>
>>> Although this has no benefit to anyone but new learners, it also
>>> doesn't really do any harm.
>>
>> I think the best we can do is to make these SyntaxWarnings. I had the
>> same thought recently and I do agree that these are common beginners
>> mistakes that can easily hide bugs by succeeding in simple tests.
>
> How would the rather common pattern of using an `object` instance as a
> placeholder be handled? An identity test precisely expresses what is
> meant and desired in that case, while an equality test does not.

It wouldn't be affected. The warning should only be emitted if either
argument to 'is' is a literal number or string. Even if x could be an
(Continue reading)

Masklinn | 8 Oct 22:14 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 2012-10-08, at 22:07 , Guido van Rossum wrote:

> On Mon, Oct 8, 2012 at 12:59 PM, Masklinn <masklinn@...> wrote:
>> On 2012-10-08, at 21:48 , Guido van Rossum wrote:
>>> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
>>>> I regularly see learners using "is" to check for string equality and
>>>> sometimes other equality. Due to optimizations, they often come away
>>>> thinking it worked for them.
>>>> 
>>>> There are no cases where
>>>> 
>>>>   if x is "foo":
>>>> 
>>>> or
>>>> 
>>>>  if x is 4:
>>>> 
>>>> is actually the code someone intended to write.
>>>> 
>>>> Although this has no benefit to anyone but new learners, it also
>>>> doesn't really do any harm.
>>> 
>>> I think the best we can do is to make these SyntaxWarnings. I had the
>>> same thought recently and I do agree that these are common beginners
>>> mistakes that can easily hide bugs by succeeding in simple tests.
>> 
>> How would the rather common pattern of using an `object` instance as a
>> placeholder be handled? An identity test precisely expresses what is
>> meant and desired in that case, while an equality test does not.
> 
(Continue reading)

Steven D'Aprano | 9 Oct 04:03 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 08, 2012 at 12:48:07PM -0700, Guido van Rossum wrote:
> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
> > I regularly see learners using "is" to check for string equality and
> > sometimes other equality. Due to optimizations, they often come away
> > thinking it worked for them.
> >
> > There are no cases where
> >
> >     if x is "foo":
> >
> > or
> >
> >    if x is 4:
> >
> > is actually the code someone intended to write.
> >
> > Although this has no benefit to anyone but new learners, it also
> > doesn't really do any harm.
> 
> I think the best we can do is to make these SyntaxWarnings. I had the
> same thought recently and I do agree that these are common beginners
> mistakes that can easily hide bugs by succeeding in simple tests.

In my experience beginners barely read error messages, let alone 
warnings.

A SyntaxWarning might help intermediate users who have graduated beyond 
the stage of "my program doesn't work, please somebody fix it", but I 
believe that at best it will be ignored by beginners, if not actively 
confuse them. And I expect that most intermediate users will have 
(Continue reading)

Guido van Rossum | 9 Oct 04:14 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 7:03 PM, Steven D'Aprano <steve@...> wrote:
> On Mon, Oct 08, 2012 at 12:48:07PM -0700, Guido van Rossum wrote:
>> On Mon, Oct 8, 2012 at 12:44 PM, Mike Graham <mikegraham@...> wrote:
>> > I regularly see learners using "is" to check for string equality and
>> > sometimes other equality. Due to optimizations, they often come away
>> > thinking it worked for them.
>> >
>> > There are no cases where
>> >
>> >     if x is "foo":
>> >
>> > or
>> >
>> >    if x is 4:
>> >
>> > is actually the code someone intended to write.
>> >
>> > Although this has no benefit to anyone but new learners, it also
>> > doesn't really do any harm.
>>
>> I think the best we can do is to make these SyntaxWarnings. I had the
>> same thought recently and I do agree that these are common beginners
>> mistakes that can easily hide bugs by succeeding in simple tests.
>
> In my experience beginners barely read error messages, let alone
> warnings.
>
> A SyntaxWarning might help intermediate users who have graduated beyond
> the stage of "my program doesn't work, please somebody fix it", but I
> believe that at best it will be ignored by beginners, if not actively
(Continue reading)

Chris Angelico | 9 Oct 09:06 2012
Picon

Re: [Python-ideas] Make "is" checks on non-singleton literals errors

On Tue, Oct 9, 2012 at 1:14 PM, Guido van Rossum <guido <at> python.org> wrote:
> Maybe we should do something more drastic and always create a new,
> unique constant whenever a literal occurs as an argument of 'is' or
> 'is not'? Then such code would never work, leading people to examine
> their code more closely. I betcha we have people who could change the
> bytecode compiler easily enough to do that. (I'm not seriously
> proposing this, except as a threat of what we could do if the
> SyntaxWarning is rejected. :-)

That wouldn't guarantee that the code would never work, merely that
the 'is' checks would be never true. Dangerous if the condition is a
guard for an unusual condition. A unit test could of course catch it,
but that assumes that everyone who writes "if x is 0" has tests
probing both branches of that check...

ChrisA
Barry Warsaw | 8 Oct 22:26 2012

Re: Make "is" checks on non-singleton literals errors

On Oct 08, 2012, at 03:44 PM, Mike Graham wrote:

>I regularly see learners using "is" to check for string equality and
>sometimes other equality. Due to optimizations, they often come away
>thinking it worked for them.
>
>There are no cases where
>
>    if x is "foo":
>
>or
>
>   if x is 4:
>
>is actually the code someone intended to write.
>
>Although this has no benefit to anyone but new learners, it also
>doesn't really do any harm.

Conversely, I often see this:

    if x == None

and even

    if x == True

Okay, so maybe these are less harmful than the original complaint, but still,
yuck!

(Continue reading)

Joshua Landau | 8 Oct 22:38 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 8 October 2012 21:26, Barry Warsaw <barry-+ZN9ApsXKcEdnm+yROfE0A@public.gmane.org> wrote:

On Oct 08, 2012, at 03:44 PM, Mike Graham wrote:

>I regularly see learners using "is" to check for string equality and
>sometimes other equality. Due to optimizations, they often come away
>thinking it worked for them.
>
>There are no cases where
>
>    if x is "foo":
>
>or
>
>   if x is 4:
>
>is actually the code someone intended to write.
>
>Although this has no benefit to anyone but new learners, it also
>doesn't really do any harm.

Conversely, I often see this:

    if x == None

and even

    if x == True

Okay, so maybe these are less harmful than the original complaint, but still,
yuck!

We can't really warn against these.

>>> class EqualToTrue:
...     def __eq__(self, other):
...             return other is True
... 
>>> EqualToTrue() is True
False
>>> EqualToTrue() == True
True
_______________________________________________
Python-ideas mailing list
Python-ideas@...
http://mail.python.org/mailman/listinfo/python-ideas
Georg Brandl | 9 Oct 22:15 2012
Picon
Picon

Re: Make "is" checks on non-singleton literals errors

Am 08.10.2012 22:38, schrieb Joshua Landau:

>     Conversely, I often see this:
> 
>         if x == None
> 
>     and even
> 
>         if x == True
> 
>     Okay, so maybe these are less harmful than the original complaint, but still,
>     yuck!
> 
> 
> We can't really warn against these.
> 
>     >>> class EqualToTrue:
>     ...     def __eq__(self, other):
>     ...             return other is True
>     ... 
>     >>> EqualToTrue() is True
>     False
>     >>> EqualToTrue() == True
>     True

The point is that in 99.9...% of cases,

  if x == True:

is just

  if x:

Georg
Antoine Pitrou | 9 Oct 22:16 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On Tue, 09 Oct 2012 22:15:42 +0200
Georg Brandl <g.brandl@...> wrote:
> 
> The point is that in 99.9...% of cases,
> 
>   if x == True:
> 
> is just
> 
>   if x:

But it's not dangerous to write `if x == True`, and so there isn't any
point in warning. As Raymond said, this is a job for a style checker.

Regards

Antoine.

--

-- 
Software development and contracting: http://pro.pitrou.net
Serhiy Storchaka | 9 Oct 23:10 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 09.10.12 23:15, Georg Brandl wrote:
> The point is that in 99.9...% of cases,
>
>    if x == True:
>
> is just
>
>    if x:

Of cause. However in Lib/unittest/main.py I found a lot of "if x != 
False:" which is not equivalent to just "if x:". It is equivalent to "if 
x is None or x:" and so I left it as is.
Arnaud Delobelle | 9 Oct 23:50 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 9 October 2012 22:10, Serhiy Storchaka <storchaka@...> wrote:
> On 09.10.12 23:15, Georg Brandl wrote:
>>
>> The point is that in 99.9...% of cases,
>>
>>    if x == True:
>>
>> is just
>>
>>    if x:
>
>
> Of cause. However in Lib/unittest/main.py I found a lot of "if x != False:"
> which is not equivalent to just "if x:". It is equivalent to "if x is None
> or x:" and so I left it as is.

???

>>> x = ''
>>> bool(x != False)
True
>>> bool(x is None or x)
False

(same with any empty sequence)

--

-- 
Arnaud
Serhiy Storchaka | 10 Oct 00:00 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 10.10.12 00:50, Arnaud Delobelle wrote:
>> Of cause. However in Lib/unittest/main.py I found a lot of "if x != False:"
>> which is not equivalent to just "if x:". It is equivalent to "if x is None
>> or x:" and so I left it as is.
>
> ???

...in context of Lib/unittest/main.py.
Georg Brandl | 10 Oct 16:21 2012
Picon
Picon

Re: Make "is" checks on non-singleton literals errors

Am 09.10.2012 23:10, schrieb Serhiy Storchaka:
> On 09.10.12 23:15, Georg Brandl wrote:
>> The point is that in 99.9...% of cases,
>>
>>    if x == True:
>>
>> is just
>>
>>    if x:
> 
> Of cause. However in Lib/unittest/main.py I found a lot of "if x != 
> False:" which is not equivalent to just "if x:". It is equivalent to "if 
> x is None or x:" and so I left it as is.

Arguably, that should be "if x is not False", but it probably doesn't
matter too much.

Georg
Serhiy Storchaka | 10 Oct 19:13 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 10.10.12 17:21, Georg Brandl wrote:
> Arguably, that should be "if x is not False", but it probably doesn't
> matter too much.

Some old code can use 1/0 instead True/False. This change will break 
such code.
Chris Angelico | 9 Oct 00:02 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On Tue, Oct 9, 2012 at 6:44 AM, Mike Graham <mikegraham@...> wrote:
> There are no cases where
>
>     if x is "foo":
>
> is actually the code someone intended to write.

Are literals guaranteed to be interned? If so, this code would make
sense, if the programmer knows that x is itself an interned string.

Although I guess a warning wouldn't be a problem there, as they're
easily ignored/suppressed.

ChrisA
Guido van Rossum | 9 Oct 00:26 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 3:02 PM, Chris Angelico <rosuav@...> wrote:
> On Tue, Oct 9, 2012 at 6:44 AM, Mike Graham <mikegraham@...> wrote:
>> There are no cases where
>>
>>     if x is "foo":
>>
>> is actually the code someone intended to write.
>
> Are literals guaranteed to be interned? If so, this code would make
> sense, if the programmer knows that x is itself an interned string.

No, interning is not guaranteed.

> Although I guess a warning wouldn't be a problem there, as they're
> easily ignored/suppressed.

--

-- 
--Guido van Rossum (python.org/~guido)
Serhiy Storchaka | 9 Oct 00:42 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 08.10.12 22:44, Mike Graham wrote:
> There are no cases where
>
>      if x is "foo":

I see such code in docutils (Doc/tools/docutils/writers/latex2e/__init__.py)

> or
>
>     if x is 4:

and in tests (Lib/test/test_long.py, Lib/test/test_int.py, 
Lib/test/test_grammar.py, Lib/test/test_winsound.py).
Guido van Rossum | 9 Oct 01:02 2012

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 3:42 PM, Serhiy Storchaka <storchaka@...> wrote:
> On 08.10.12 22:44, Mike Graham wrote:
>>
>> There are no cases where
>>
>>      if x is "foo":
>
> I see such code in docutils (Doc/tools/docutils/writers/latex2e/__init__.py)

And that's probably a bug.

>> or
>>
>>     if x is 4:
>
> and in tests (Lib/test/test_long.py, Lib/test/test_int.py,
> Lib/test/test_grammar.py, Lib/test/test_winsound.py).

The tests are easily rewritten using

four = 4
if x is four:
  ...

--

-- 
--Guido van Rossum (python.org/~guido)
Mike Graham | 9 Oct 01:05 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 6:42 PM, Serhiy Storchaka <storchaka@...> wrote:
> On 08.10.12 22:44, Mike Graham wrote:
>>
>> There are no cases where
>>
>>      if x is "foo":
>
>
> I see such code in docutils (Doc/tools/docutils/writers/latex2e/__init__.py)

Thanks for finding these!

I can't find this in a couple versions of Python I checked. If this
code is still around, it sounds like it has a bug and should be fixed.

>> or
>>
>>     if x is 4:
>
>
> and in tests (Lib/test/test_long.py, Lib/test/test_int.py,
> Lib/test/test_grammar.py, Lib/test/test_winsound.py).

test_grammar.py is correct, but trivially so. It merely ensures that
`1 is 1` and `1 is not 1` are proper Python syntax. As we're talking
about tweaking Python's syntax rules, obviously code that tests that
the grammar is the current thing would use the current thing.

test_int.py and test_long.py are valid but unique, in that they rely
on the behavior that no other code should implicitly rely on to test
an implementation detail

test_winsound.py has an `is 0` check and an `is ""` check. Both should be fixed.

Thanks again,
Mike
Serhiy Storchaka | 9 Oct 11:58 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 09.10.12 02:05, Mike Graham wrote:
> I can't find this in a couple versions of Python I checked. If this
> code is still around, it sounds like it has a bug and should be fixed.

It's "if node.tagname is 'admonition':" line.

> test_winsound.py has an `is 0` check and an `is ""` check. Both should be fixed.

http://bugs.python.org/issue16172
Georg Brandl | 9 Oct 22:16 2012
Picon
Picon

Re: Make "is" checks on non-singleton literals errors

Am 09.10.2012 11:58, schrieb Serhiy Storchaka:
> On 09.10.12 02:05, Mike Graham wrote:
>> I can't find this in a couple versions of Python I checked. If this
>> code is still around, it sounds like it has a bug and should be fixed.
> 
> It's "if node.tagname is 'admonition':" line.

It's not part of Python anyway, and should be reported to the docutils
maintainers.

Georg
Serhiy Storchaka | 10 Oct 00:00 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On 09.10.12 23:16, Georg Brandl wrote:
> It's not part of Python anyway, and should be reported to the docutils
> maintainers.

Done.
Raymond Hettinger | 9 Oct 03:13 2012
Picon

Re: Make "is" checks on non-singleton literals errors


On Oct 8, 2012, at 12:44 PM, Mike Graham <mikegraham@...> wrote:

> I regularly see learners using "is" to check for string equality and
> sometimes other equality. Due to optimizations, they often come away
> thinking it worked for them.
> 
> There are no cases where
> 
>    if x is "foo":
> 
> or
> 
>   if x is 4:
> 
> is actually the code someone intended to write.
> 
> Although this has no benefit to anyone but new learners, it also
> doesn't really do any harm.

This seems like a job for pyflakes, pylint, or pychecker.

Raymond
Barry Warsaw | 9 Oct 17:15 2012

Re: Make "is" checks on non-singleton literals errors

On Oct 08, 2012, at 06:13 PM, Raymond Hettinger wrote:

>
>On Oct 8, 2012, at 12:44 PM, Mike Graham <mikegraham@...> wrote:
>
>> I regularly see learners using "is" to check for string equality and
>> sometimes other equality. Due to optimizations, they often come away
>> thinking it worked for them.
>> 
>> There are no cases where
>> 
>>    if x is "foo":
>> 
>> or
>> 
>>   if x is 4:
>> 
>> is actually the code someone intended to write.
>> 
>> Although this has no benefit to anyone but new learners, it also
>> doesn't really do any harm.
>
>This seems like a job for pyflakes, pylint, or pychecker.

+1

-Barry
_______________________________________________
Python-ideas mailing list
Python-ideas@...
http://mail.python.org/mailman/listinfo/python-ideas
Calvin Spealman | 9 Oct 03:14 2012
Picon

Re: Make "is" checks on non-singleton literals errors

On Mon, Oct 8, 2012 at 3:44 PM, Mike Graham <mikegraham@...> wrote:
>
> I regularly see learners using "is" to check for string equality and
> sometimes other equality. Due to optimizations, they often come away
> thinking it worked for them.
>
> There are no cases where
>
>     if x is "foo":
>
> or
>
>    if x is 4:
>
> is actually the code someone intended to write.
>
> Although this has no benefit to anyone but new learners, it also
> doesn't really do any harm.

+1

> Mike
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@...
> http://mail.python.org/mailman/listinfo/python-ideas

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

Gmane