[Python-checkins] r57598 - in sandbox/trunk/2to3: README fixes/fix_type_equality.py tests/test_fixers.py

collin.winter python-checkins at python.org
Tue Aug 28 07:47:47 CEST 2007


Author: collin.winter
Date: Tue Aug 28 07:47:47 2007
New Revision: 57598

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/README
   sandbox/trunk/2to3/fixes/fix_type_equality.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add support for negative comparisons to fix_type_equality.


Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README	(original)
+++ sandbox/trunk/2to3/README	Tue Aug 28 07:47:47 2007
@@ -84,7 +84,7 @@
 * **fix_tuple_params** - remove tuple parameters from function, method and
   lambda declarations (PEP 3113).
 
-* **fix_type_equality** - convert type(x) == T to isinstance(x, T).
+* **fix_type_equality** - convert type(x) == T to isinstance(x, T), etc.
   
 * **fix_unicode** - convert, e.g., u"..." to "...", unicode(x) to str(x), etc.
   

Modified: sandbox/trunk/2to3/fixes/fix_type_equality.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_type_equality.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_type_equality.py	Tue Aug 28 07:47:47 2007
@@ -2,25 +2,32 @@
 
 type(x) == T -> isinstance(x, T)
 type(x) is T -> isinstance(x, T)
+type(x) != T -> not isinstance(x, T)
+type(x) is not T -> not isinstance(x, T)
 """
 # Author: Jacques Frechet
 
 # Local imports
 from fixes import basefix
-from fixes.util import Call, Comma, Name
+from fixes.util import Call, Comma, Name, Node, syms
 
+CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)"
 
 class FixTypeEquality(basefix.BaseFix):
 
     PATTERN = """
-        comparison< power< 'type' trailer< '(' x=any ')' > > ('==' | 'is') T=any > |
-        comparison< T=any ('==' | 'is') power< 'type' trailer< '(' x=any ')' > > >
-    """
+        comparison< power< 'type' trailer< '(' x=any ')' > > %s T=any > |
+        comparison< T=any %s power< 'type' trailer< '(' x=any ')' > > >
+    """ % (CMP, CMP)
 
     def transform(self, node, results):
         x = results['x'].clone() # The thing inside of type()
         T = results['T'].clone() # The type being compared against
         x.set_prefix('')
         T.set_prefix(' ')
-        return Call(Name('isinstance', prefix=node.get_prefix()),
-                    [x, Comma(), T])
+        test = Call(Name('isinstance'), [x, Comma(), T])
+        if "n" in results:
+            test.set_prefix(" ")
+            test = Node(syms.not_test, [Name('not'), test])
+        test.set_prefix(node.get_prefix())
+        return test

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Tue Aug 28 07:47:47 2007
@@ -2405,6 +2405,60 @@
         a = """isinstance(x  +  y, d.get('T'))"""
         self.check(b, a)
 
+    def test_is_not_simple(self):
+        b = """type(x) is not T"""
+        a = """not isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   type(x) is not T: pass"""
+        a = """if   not isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_is_not_reverse(self):
+        b = """T is not type(x)"""
+        a = """not isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   T is not type(x): pass"""
+        a = """if   not isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_is_not_expression(self):
+        b = """type(x+y) is not d.get('T')"""
+        a = """not isinstance(x+y, d.get('T'))"""
+        self.check(b, a)
+
+        b = """type(   x  +  y) is not d.get('T')"""
+        a = """not isinstance(x  +  y, d.get('T'))"""
+        self.check(b, a)
+
+    def test_ne_simple(self):
+        b = """type(x) != T"""
+        a = """not isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   type(x) != T: pass"""
+        a = """if   not isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_ne_reverse(self):
+        b = """T != type(x)"""
+        a = """not isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   T != type(x): pass"""
+        a = """if   not isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_ne_expression(self):
+        b = """type(x+y) != d.get('T')"""
+        a = """not isinstance(x+y, d.get('T'))"""
+        self.check(b, a)
+
+        b = """type(   x  +  y) != d.get('T')"""
+        a = """not isinstance(x  +  y, d.get('T'))"""
+        self.check(b, a)
+
     def test_unchanged(self):
         a = """type(x).__name__"""
         self.unchanged(a)


More information about the Python-checkins mailing list