OLD | NEW |
(Empty) | |
| 1 u""" |
| 2 Fixer to remove function annotations |
| 3 """ |
| 4 |
| 5 from lib2to3 import fixer_base |
| 6 from lib2to3.pgen2 import token |
| 7 from lib2to3.fixer_util import syms |
| 8 |
| 9 warning_text = u"Removing function annotations completely." |
| 10 |
| 11 def param_without_annotations(node): |
| 12 return node.children[0] |
| 13 |
| 14 class FixAnnotations(fixer_base.BaseFix): |
| 15 |
| 16 warned = False |
| 17 |
| 18 def warn_once(self, node, reason): |
| 19 if not self.warned: |
| 20 self.warned = True |
| 21 self.warning(node, reason=reason) |
| 22 |
| 23 PATTERN = u""" |
| 24 funcdef< 'def' any parameters< '(' [params=any] ')' > ['->' ret=an
y] ':' any* > |
| 25 """ |
| 26 |
| 27 def transform(self, node, results): |
| 28 u""" |
| 29 This just strips annotations from the funcdef completely. |
| 30 """ |
| 31 params = results.get(u"params") |
| 32 ret = results.get(u"ret") |
| 33 if ret is not None: |
| 34 assert ret.prev_sibling.type == token.RARROW, u"Invalid return annot
ation" |
| 35 self.warn_once(node, reason=warning_text) |
| 36 ret.prev_sibling.remove() |
| 37 ret.remove() |
| 38 if params is None: return |
| 39 if params.type == syms.typedargslist: |
| 40 # more than one param in a typedargslist |
| 41 for param in params.children: |
| 42 if param.type == syms.tname: |
| 43 self.warn_once(node, reason=warning_text) |
| 44 param.replace(param_without_annotations(param)) |
| 45 elif params.type == syms.tname: |
| 46 # one param |
| 47 self.warn_once(node, reason=warning_text) |
| 48 params.replace(param_without_annotations(params)) |
OLD | NEW |