fix_methodattrs.py 606 B

123456789101112131415161718192021222324
  1. """Fix bound method attributes (method.im_? -> method.__?__).
  2. """
  3. # Author: Christian Heimes
  4. # Local imports
  5. from .. import fixer_base
  6. from ..fixer_util import Name
  7. MAP = {
  8. "im_func" : "__func__",
  9. "im_self" : "__self__",
  10. "im_class" : "__self__.__class__"
  11. }
  12. class FixMethodattrs(fixer_base.BaseFix):
  13. BM_compatible = True
  14. PATTERN = """
  15. power< any+ trailer< '.' attr=('im_func' | 'im_self' | 'im_class') > any* >
  16. """
  17. def transform(self, node, results):
  18. attr = results["attr"][0]
  19. new = MAP[attr.value]
  20. attr.replace(Name(new, prefix=attr.prefix))