PageRenderTime 32ms CodeModel.GetById 25ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/lib2to3/fixes/fix_types.py

http://unladen-swallow.googlecode.com/
Python | 62 lines | 36 code | 2 blank | 24 comment | 0 complexity | 08728aeba77665139ce3f967cb24c2f1 MD5 | raw file
 1# Copyright 2007 Google, Inc. All Rights Reserved.
 2# Licensed to PSF under a Contributor Agreement.
 3
 4"""Fixer for removing uses of the types module.
 5
 6These work for only the known names in the types module.  The forms above
 7can include types. or not.  ie, It is assumed the module is imported either as:
 8
 9    import types
10    from types import ... # either * or specific types
11
12The import statements are not modified.
13
14There should be another fixer that handles at least the following constants:
15
16   type([]) -> list
17   type(()) -> tuple
18   type('') -> str
19
20"""
21
22# Local imports
23from ..pgen2 import token
24from .. import fixer_base
25from ..fixer_util import Name
26
27_TYPE_MAPPING = {
28        'BooleanType' : 'bool',
29        'BufferType' : 'memoryview',
30        'ClassType' : 'type',
31        'ComplexType' : 'complex',
32        'DictType': 'dict',
33        'DictionaryType' : 'dict',
34        'EllipsisType' : 'type(Ellipsis)',
35        #'FileType' : 'io.IOBase',
36        'FloatType': 'float',
37        'IntType': 'int',
38        'ListType': 'list',
39        'LongType': 'int',
40        'ObjectType' : 'object',
41        'NoneType': 'type(None)',
42        'NotImplementedType' : 'type(NotImplemented)',
43        'SliceType' : 'slice',
44        'StringType': 'bytes', # XXX ?
45        'StringTypes' : 'str', # XXX ?
46        'TupleType': 'tuple',
47        'TypeType' : 'type',
48        'UnicodeType': 'str',
49        'XRangeType' : 'range',
50    }
51
52_pats = ["power< 'types' trailer< '.' name='%s' > >" % t for t in _TYPE_MAPPING]
53
54class FixTypes(fixer_base.BaseFix):
55
56    PATTERN = '|'.join(_pats)
57
58    def transform(self, node, results):
59        new_value = _TYPE_MAPPING.get(results["name"].value)
60        if new_value:
61            return Name(new_value, prefix=node.get_prefix())
62        return None