1
# Copyright 2008 Armin Ronacher.
2
# Licensed to PSF under a Contributor Agreement.
4
"""Fixer that cleans up a tuple argument to isinstance after the tokens
5
in it were fixed. This is mainly used to remove double occurrences of
6
tokens as a leftover of the long -> int / unicode -> str conversion.
8
eg. isinstance(x, (int, long)) -> isinstance(x, (int, int))
12
from .. import fixer_base
13
from ..fixer_util import token
16
class FixIsinstance(fixer_base.BaseFix):
21
trailer< '(' arglist< any ',' atom< '('
22
args=testlist_gexp< any+ >
29
def transform(self, node, results):
30
names_inserted = set()
31
testlist = results["args"]
32
args = testlist.children
34
iterator = enumerate(args)
35
for idx, arg in iterator:
36
if arg.type == token.NAME and arg.value in names_inserted:
37
if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:
42
if arg.type == token.NAME:
43
names_inserted.add(arg.value)
44
if new_args and new_args[-1].type == token.COMMA:
46
if len(new_args) == 1:
47
atom = testlist.parent
48
new_args[0].set_prefix(atom.get_prefix())
49
atom.replace(new_args[0])