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

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