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

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

#countcontent
1n/a# Copyright 2008 Armin Ronacher.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Fixer that cleans up a tuple argument to isinstance after the tokens
5n/ain it were fixed. This is mainly used to remove double occurrences of
6n/atokens as a leftover of the long -> int / unicode -> str conversion.
7n/a
8n/aeg. isinstance(x, (int, long)) -> isinstance(x, (int, int))
9n/a -> isinstance(x, int)
10n/a"""
11n/a
12n/afrom .. import fixer_base
13n/afrom ..fixer_util import token
14n/a
15n/a
16n/aclass FixIsinstance(fixer_base.BaseFix):
17n/a BM_compatible = True
18n/a PATTERN = """
19n/a power<
20n/a 'isinstance'
21n/a trailer< '(' arglist< any ',' atom< '('
22n/a args=testlist_gexp< any+ >
23n/a ')' > > ')' >
24n/a >
25n/a """
26n/a
27n/a run_order = 6
28n/a
29n/a def transform(self, node, results):
30n/a names_inserted = set()
31n/a testlist = results["args"]
32n/a args = testlist.children
33n/a new_args = []
34n/a iterator = enumerate(args)
35n/a for idx, arg in iterator:
36n/a if arg.type == token.NAME and arg.value in names_inserted:
37n/a if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:
38n/a next(iterator)
39n/a continue
40n/a else:
41n/a new_args.append(arg)
42n/a if arg.type == token.NAME:
43n/a names_inserted.add(arg.value)
44n/a if new_args and new_args[-1].type == token.COMMA:
45n/a del new_args[-1]
46n/a if len(new_args) == 1:
47n/a atom = testlist.parent
48n/a new_args[0].prefix = atom.prefix
49n/a atom.replace(new_args[0])
50n/a else:
51n/a args[:] = new_args
52n/a node.changed()