| 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 callable(). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | This converts callable(obj) into isinstance(obj, collections.Callable), adding a |
|---|
| 7 | n/a | collections import if needed.""" |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | # Local imports |
|---|
| 10 | n/a | from lib2to3 import fixer_base |
|---|
| 11 | n/a | from lib2to3.fixer_util import Call, Name, String, Attr, touch_import |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | class FixCallable(fixer_base.BaseFix): |
|---|
| 14 | n/a | BM_compatible = True |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | order = "pre" |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | # Ignore callable(*args) or use of keywords. |
|---|
| 19 | n/a | # Either could be a hint that the builtin callable() is not being used. |
|---|
| 20 | n/a | PATTERN = """ |
|---|
| 21 | n/a | power< 'callable' |
|---|
| 22 | n/a | trailer< lpar='(' |
|---|
| 23 | n/a | ( not(arglist | argument<any '=' any>) func=any |
|---|
| 24 | n/a | | func=arglist<(not argument<any '=' any>) any ','> ) |
|---|
| 25 | n/a | rpar=')' > |
|---|
| 26 | n/a | after=any* |
|---|
| 27 | n/a | > |
|---|
| 28 | n/a | """ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def transform(self, node, results): |
|---|
| 31 | n/a | func = results['func'] |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | touch_import(None, 'collections', node=node) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | args = [func.clone(), String(', ')] |
|---|
| 36 | n/a | args.extend(Attr(Name('collections'), Name('Callable'))) |
|---|
| 37 | n/a | return Call(Name('isinstance'), args, prefix=node.prefix) |
|---|