/Lib/lib2to3/fixes/fix_callable.py

http://unladen-swallow.googlecode.com/ · Python · 31 lines · 26 code · 1 blank · 4 comment · 0 complexity · 37990663703ff5ea2fabb3095a9ad189 MD5 · raw file

  1. # Copyright 2007 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for callable().
  4. This converts callable(obj) into hasattr(obj, '__call__')."""
  5. # Local imports
  6. from .. import pytree
  7. from .. import fixer_base
  8. from ..fixer_util import Call, Name, String
  9. class FixCallable(fixer_base.BaseFix):
  10. # Ignore callable(*args) or use of keywords.
  11. # Either could be a hint that the builtin callable() is not being used.
  12. PATTERN = """
  13. power< 'callable'
  14. trailer< lpar='('
  15. ( not(arglist | argument<any '=' any>) func=any
  16. | func=arglist<(not argument<any '=' any>) any ','> )
  17. rpar=')' >
  18. after=any*
  19. >
  20. """
  21. def transform(self, node, results):
  22. func = results["func"]
  23. args = [func.clone(), String(', '), String("'__call__'")]
  24. return Call(Name("hasattr"), args, prefix=node.get_prefix())