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

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


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

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/fixes/fix_type_equality.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add support for 'is'-based comparisons to fix_type_equality.


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:31 2007
@@ -1,4 +1,8 @@
-"""Fix type(x) == T -> isinstance(x, T)."""
+"""Change some type comparisons to isinstance() calls:
+
+type(x) == T -> isinstance(x, T)
+type(x) is T -> isinstance(x, T)
+"""
 # Author: Jacques Frechet
 
 # Local imports
@@ -9,8 +13,8 @@
 class FixTypeEquality(basefix.BaseFix):
 
     PATTERN = """
-        comparison< power< 'type' trailer< '(' x=any ')' > > '==' T=any > |
-        comparison< T=any '==' power< 'type' trailer< '(' x=any ')' > > >
+        comparison< power< 'type' trailer< '(' x=any ')' > > ('==' | 'is') T=any > |
+        comparison< T=any ('==' | 'is') power< 'type' trailer< '(' x=any ')' > > >
     """
 
     def transform(self, node, results):

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:31 2007
@@ -2351,7 +2351,7 @@
 class Test_type_equality(FixerTestCase):
     fixer = "type_equality"
 
-    def test_simple(self):
+    def test_eq_simple(self):
         b = """type(x) == T"""
         a = """isinstance(x, T)"""
         self.check(b, a)
@@ -2360,7 +2360,7 @@
         a = """if   isinstance(x, T): pass"""
         self.check(b, a)
 
-    def test_reverse(self):
+    def test_eq_reverse(self):
         b = """T == type(x)"""
         a = """isinstance(x, T)"""
         self.check(b, a)
@@ -2369,7 +2369,7 @@
         a = """if   isinstance(x, T): pass"""
         self.check(b, a)
 
-    def test_expression(self):
+    def test_eq_expression(self):
         b = """type(x+y) == d.get('T')"""
         a = """isinstance(x+y, d.get('T'))"""
         self.check(b, a)
@@ -2378,6 +2378,33 @@
         a = """isinstance(x  +  y, d.get('T'))"""
         self.check(b, a)
 
+    def test_is_simple(self):
+        b = """type(x) is T"""
+        a = """isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   type(x) is T: pass"""
+        a = """if   isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_is_reverse(self):
+        b = """T is type(x)"""
+        a = """isinstance(x, T)"""
+        self.check(b, a)
+
+        b = """if   T is type(x): pass"""
+        a = """if   isinstance(x, T): pass"""
+        self.check(b, a)
+
+    def test_is_expression(self):
+        b = """type(x+y) is d.get('T')"""
+        a = """isinstance(x+y, d.get('T'))"""
+        self.check(b, a)
+
+        b = """type(   x  +  y) is d.get('T')"""
+        a = """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