The 'is' operator and check-code

Frank Kingswood frank at kingswood-consulting.co.uk
Wed Nov 17 05:09:32 CST 2010


On 16/11/10 19:40, Matt Mackall wrote:
> I'm throwing this out in case any Python experts have opinions.
>
> The 'is' operator is used in a bunch of places as a synonym for ==. This
> happens to work with short strings and small integers (0 to 256) in
> CPython, but that's by no means guaranteed by the language, so there are
> places where we're just getting lucky. So I propose we disallow using it
> for anything that's not one of the True/False/None singletons.

This seems wrong. In some places we're interested in checking two are indeed 
the same object, not that they have the same contents.
If you replace the "a is b" check with "a==b" and then someone adds an 
__equal__ method to the class of which a and b are instances then the 
resulting code may behave in a different way (and also probably be slower).

So in your list:

> hgext/convert/cvsps.py:621 (benoit at 8456):
>   >          c.tags = sorted(tag for tag in tags if globaltags[tag] is c)
>   object comparison with non-singleton
> hgext/patchbomb.py:476 (pmezard at 4599):
>   >              if fp is not ui:
>   object comparison with non-singleton

look like straight object-is-the-same-instance comparisons

> mercurial/fancyopts.py:105 (mpm at 5638):
>   >          if t is type(fancyopts):
>   object comparison with non-singleton
> mercurial/fancyopts.py:107 (mpm at 5638):
>   >          elif t is type(1):
>   object comparison with non-singleton
> mercurial/fancyopts.py:109 (mpm at 5638):
>   >          elif t is type(''):
>   object comparison with non-singleton
> mercurial/fancyopts.py:111 (mpm at 5638):
>   >          elif t is type([]):
>   object comparison with non-singleton
> mercurial/fancyopts.py:113 (mpm at 5638):
>   >          elif t is type(None) or t is type(False):
>   object comparison with non-singleton

these look like they should be isinstance()
But the others should probably be ==

Because of this I'm not sure it is reasonable to add such a check to the 
check-code script.

Frank



More information about the Mercurial-devel mailing list