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