+++ /dev/null
-# Copyright 2008 Armin Ronacher.\r
-# Licensed to PSF under a Contributor Agreement.\r
-\r
-"""Fixer that cleans up a tuple argument to isinstance after the tokens\r
-in it were fixed. This is mainly used to remove double occurrences of\r
-tokens as a leftover of the long -> int / unicode -> str conversion.\r
-\r
-eg. isinstance(x, (int, long)) -> isinstance(x, (int, int))\r
- -> isinstance(x, int)\r
-"""\r
-\r
-from .. import fixer_base\r
-from ..fixer_util import token\r
-\r
-\r
-class FixIsinstance(fixer_base.BaseFix):\r
- BM_compatible = True\r
- PATTERN = """\r
- power<\r
- 'isinstance'\r
- trailer< '(' arglist< any ',' atom< '('\r
- args=testlist_gexp< any+ >\r
- ')' > > ')' >\r
- >\r
- """\r
-\r
- run_order = 6\r
-\r
- def transform(self, node, results):\r
- names_inserted = set()\r
- testlist = results["args"]\r
- args = testlist.children\r
- new_args = []\r
- iterator = enumerate(args)\r
- for idx, arg in iterator:\r
- if arg.type == token.NAME and arg.value in names_inserted:\r
- if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:\r
- iterator.next()\r
- continue\r
- else:\r
- new_args.append(arg)\r
- if arg.type == token.NAME:\r
- names_inserted.add(arg.value)\r
- if new_args and new_args[-1].type == token.COMMA:\r
- del new_args[-1]\r
- if len(new_args) == 1:\r
- atom = testlist.parent\r
- new_args[0].prefix = atom.prefix\r
- atom.replace(new_args[0])\r
- else:\r
- args[:] = new_args\r
- node.changed()\r