Debian Codegraph @

cpython27/Lib/lib2to3/fixes/fix_types.py

raw symbols SCIP
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
6 These work for only the known names in the types module. The forms above
7 can 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
12 The import statements are not modified.
13
14 There 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
23 from ..pgen2 import token
24 from .. import fixer_base
25 from ..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
54 class FixTypes(fixer_base.BaseFix):
55 BM_compatible = True
56 PATTERN = '|'.join(_pats)
57
58 def transform(self, node, results):
59 new_value = unicode(_TYPE_MAPPING.get(results["name"].value))
60 if new_value:
61 return Name(new_value, prefix=node.prefix)
62 return None