fix_nonzero.py 591 B

123456789101112131415161718192021
  1. """Fixer for __nonzero__ -> __bool__ methods."""
  2. # Author: Collin Winter
  3. # Local imports
  4. from .. import fixer_base
  5. from ..fixer_util import Name
  6. class FixNonzero(fixer_base.BaseFix):
  7. BM_compatible = True
  8. PATTERN = """
  9. classdef< 'class' any+ ':'
  10. suite< any*
  11. funcdef< 'def' name='__nonzero__'
  12. parameters< '(' NAME ')' > any+ >
  13. any* > >
  14. """
  15. def transform(self, node, results):
  16. name = results["name"]
  17. new = Name("__bool__", prefix=name.prefix)
  18. name.replace(new)