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

neal.norwitz python-checkins at python.org
Thu Aug 23 00:53:50 CEST 2007


Author: neal.norwitz
Date: Thu Aug 23 00:53:50 2007
New Revision: 57297

Added:
   sandbox/trunk/2to3/fixes/fix_types.py
Modified:
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add a fixer to change the types that are builtin to use the builtin name
rather than the name in the types module.


Added: sandbox/trunk/2to3/fixes/fix_types.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_types.py	Thu Aug 23 00:53:50 2007
@@ -0,0 +1,49 @@
+# Copyright 2007 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer for removing uses of the types module.
+
+These work for only the known names in the types module.  The forms above
+can include types. or not.  ie, It is assumed the module is imported either as:
+
+    import types
+    from types import ... # either * or specific types
+
+The import statements are not modified.
+
+There should be another fixer that handles at least the following constants:
+
+   type([]) -> list
+   type(()) -> tuple
+   type('') -> str
+
+"""
+
+# Local imports
+from pgen2 import token
+from fixes import basefix
+from fixes.util import Name
+
+_TYPE_MAPPING = {
+        'DictType': 'dict',
+        'ListType': 'list',
+        'NoneType': 'type(None)',
+        'IntType': 'int',
+        'LongType': 'int',
+        'FloatType': 'float',
+        'StringType': 'str',
+        'TupleType': 'tuple',
+        'UnicodeType': 'unicode',
+    }
+
+_pats = ["power< 'types' trailer< '.' name='%s' > >" % t for t in _TYPE_MAPPING]
+
+class FixTypes(basefix.BaseFix):
+
+    PATTERN = '|'.join(_pats)
+
+    def transform(self, node, results):
+        new_value = _TYPE_MAPPING.get(results["name"].value)
+        if new_value:
+            return Name(new_value, prefix=node.get_prefix())
+        return None

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Thu Aug 23 00:53:50 2007
@@ -2308,6 +2308,30 @@
             """
         self.warns_unchanged(a, "You should use a for loop here")
 
+class Test_types(FixerTestCase):
+    fixer = "types"
+
+    def test_basic_types_convert(self):
+        b = """types.StringType"""
+        a = """str"""
+        self.check(b, a)
+
+        b = """types.DictType"""
+        a = """dict"""
+        self.check(b, a)
+
+        b = """types . IntType"""
+        a = """int"""
+        self.check(b, a)
+
+        b = """types.ListType"""
+        a = """list"""
+        self.check(b, a)
+
+        b = """types.LongType"""
+        a = """int"""
+        self.check(b, a)
+
 
 if __name__ == "__main__":
     import __main__


More information about the Python-checkins mailing list