Skip to content

Commit bc86b12

Browse files
andrerfcsantosBethanyG
authored andcommitted
Make representer ignore type annotations in more places
1 parent bc49fb6 commit bc86b12

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

representer/normalizer.py

+16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from itertools import count
66
from typing import Dict, Optional, overload
77
from ast import (
8+
AnnAssign,
89
AST,
10+
Assign,
911
AsyncFunctionDef,
1012
Attribute,
1113
ClassDef,
@@ -113,8 +115,22 @@ def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef:
113115
"""
114116
Any `def name` definition.
115117
"""
118+
if node.returns:
119+
node.returns = None
116120
return self._visit_definition(node)
117121

122+
def visit_AnnAssign(self, node: AnnAssign) -> Assign:
123+
"""
124+
Any type-annotated assignment
125+
126+
Converts type-annotated assignments to regular assignments.
127+
"""
128+
new_assign = Assign(targets=[node.target],
129+
value=node.value,
130+
lineno=node.lineno)
131+
self.generic_visit(new_assign)
132+
return new_assign
133+
118134
def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> AsyncFunctionDef:
119135
"""
120136
Any `async def name` definition.

test/test_representer.py

+24
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,30 @@ def placeholder_0(placeholder_1, placeholder_2=None,
303303
"""
304304
self.assertCodeEqual(before, expect)
305305

306+
def test_def_functions_with_annotations_in_signature(self):
307+
before = """\
308+
def func(a: int, b: int) -> int:
309+
return a + b
310+
"""
311+
expect = """\
312+
def placeholder_0(placeholder_1, placeholder_2):
313+
return placeholder_1 + placeholder_2
314+
"""
315+
self.assertCodeEqual(before, expect)
316+
317+
def test_def_functions_with_annotations_in_body(self):
318+
before = """\
319+
def func(a: int, b: int) -> int:
320+
c: int = 2
321+
return a + b + c
322+
"""
323+
expect = """\
324+
def placeholder_0(placeholder_1, placeholder_2):
325+
placeholder_3 = 2
326+
return placeholder_1 + placeholder_2 + placeholder_3
327+
"""
328+
self.assertCodeEqual(before, expect)
329+
306330
def test_async_def_functions(self):
307331
before = """\
308332
async def afunc(posarg, defarg=None, *varargs, **nameargs):

0 commit comments

Comments
 (0)