Debian Codegraph @

lib-python/3/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 .. import fixer_base
24 from ..fixer_util import Name
25
26 _TYPE_MAPPING = {
27 'BooleanType' : 'bool',
28 'BufferType' : 'memoryview',
29 'ClassType' : 'type',
30 'ComplexType' : 'complex',
31 'DictType': 'dict',
32 'DictionaryType' : 'dict',
33 'EllipsisType' : 'type(Ellipsis)',
34 #'FileType' : 'io.IOBase',
35 'FloatType': 'float',
36 'IntType': 'int',
37 'ListType': 'list',
38 'LongType': 'int',
39 'ObjectType' : 'object',
40 'NoneType': 'type(None)',
41 'NotImplementedType' : 'type(NotImplemented)',
42 'SliceType' : 'slice',
43 'StringType': 'bytes', # XXX ?
44 'StringTypes' : '(str,)', # XXX ?
45 'TupleType': 'tuple',
46 'TypeType' : 'type',
47 'UnicodeType': 'str',
48 'XRangeType' : 'range',
49 }
50
51 _pats = ["power< 'types' trailer< '.' name='%s' > >" % t for t in _TYPE_MAPPING]
52
53 class FixTypes(fixer_base.BaseFix):
54 BM_compatible = True
55 PATTERN = '|'.join(_pats)
56
57 def transform(self, node, results):
58 new_value = _TYPE_MAPPING.get(results["name"].value)
59 if new_value:
60 return Name(new_value, prefix=node.prefix)
61 return None