The 'is' operator and check-code

Matt Mackall mpm at selenic.com
Wed Nov 17 13:54:15 CST 2010


On Wed, 2010-11-17 at 11:33 +0100, Martin Geisler wrote:
> Adrian Buehlmann <adrian at cadifra.com> writes:
> 
> > On 16.11.2010 20: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. 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)\(',
> >> 
> >
> > and elsewhere:
> >
> > On 17.11.2010 09:05, Martin Geisler wrote:
> >> Mads Kiilerich <mads at kiilerich.com> writes:
> >>
> >>> I won't claim to be a Python expert, but I don't think the
> >>> check-code check proposed by Matt is a good idea. It can't be made
> >>> reliable - unless we say that all uses of 'is' has to be
> >>> white-listed somehow.
> >>
> >> I propose that we just blacklist using of 'is' with string and
> >> numeric literals. That would be something like this:
> >>
> >>   (r''' is\s+(not\s+)?["'0-9]''', "object comparison with literal"),
> >
> > Would it be possible to catch negative numbers too?
> 
> Good idea, inserting '-' at the end of the range will do that.
> 
> I'll wait for feedback from Matt and Dan.

It's progress.

Relatedly, we should probably not be doing the following:

i18n/polib.py:    if kwargs.get('autodetect_encoding', True) == True:
mercurial/revset.py:    if x == None:

'if x == True/False/None' should always either be 'if (not) x:' or 'if x
is True/False/None'.

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list