The 'is' operator and check-code

Matt Mackall mpm at selenic.com
Tue Nov 16 13:40:46 CST 2010


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. Here's my
regex hack:

diff -r 340c46028ebd contrib/check-code.py
--- a/contrib/check-code.py	Tue Nov 16 12:55:07 2010 -0600
+++ b/contrib/check-code.py	Tue Nov 16 13:35:04 2010 -0600
@@ -130,6 +130,7 @@
     (r'[\x80-\xff]', "non-ASCII character literal"),
     (r'("\')\.format\(', "str.format() not available in Python 2.4"),
     (r'^\s*with\s+', "with not available in Python 2.4"),
+    (r' is\s+(not\s+)?[^TFNn]', "object comparison with non-singleton"),
     (r'(?<!def)\s+(any|all|format)\(',
      "any/all/format not available in Python 2.4"),
     (r'(?<!def)\s+(callable)\(',


It gives the following results:

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
mercurial/dispatch.py:203 (brodie at 12039):
 >                 if e is entry:
 object comparison with non-singleton
mercurial/extensions.py:123 (mpm at 7215):
 >         if e is entry:
 object comparison with non-singleton
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
mercurial/httprepo.py:71 (mpm at 12969):
 >         if cmd is 'pushkey':
 object comparison with non-singleton
mercurial/posix.py:147 (danchr at 9238):
 >             if err.errno is errno.ENOENT:
 object comparison with non-singleton
mercurial/url.py:321 (hg at 10409):
 >                     if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
 object comparison with non-singleton
mercurial/url.py:596 (mads at 11457):
 >         if req is not self.retried_req:
 object comparison with non-singleton
mercurial/url.py:624 (wbruna at 11844):
 >         if req is not self.retried_req:
 object comparison with non-singleton
tests/tinyproxy.py:99 (vadim at 2337):
 >                     if i is soc:
 object comparison with non-singleton


-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list