ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_types.py

Python code coverage for Lib/lib2to3/fixes/fix_types.py

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