Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ Release Date: TBA

Close PyCQA/pylint#3904

* Corrected the parent of function type comment nodes.

These nodes used to be parented to their original ast.FunctionDef parent
but are now correctly parented to their astroid.FunctionDef parent.

Close PyCQA/astroid#851


What's New in astroid 2.4.2?
============================
Expand Down
8 changes: 4 additions & 4 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def check_type_comment(self, node, parent):

return type_object.value

def check_function_type_comment(self, node):
def check_function_type_comment(self, node, parent):
type_comment = getattr(node, "type_comment", None)
if not type_comment:
return None
Expand All @@ -251,10 +251,10 @@ def check_function_type_comment(self, node):

returns = None
argtypes = [
self.visit(elem, node) for elem in (type_comment_ast.argtypes or [])
self.visit(elem, parent) for elem in (type_comment_ast.argtypes or [])
]
if type_comment_ast.returns:
returns = self.visit(type_comment_ast.returns, node)
returns = self.visit(type_comment_ast.returns, parent)

return returns, argtypes

Expand Down Expand Up @@ -615,7 +615,7 @@ def _visit_functiondef(self, cls, node, parent):
returns = None

type_comment_args = type_comment_returns = None
type_comment_annotation = self.check_function_type_comment(node)
type_comment_annotation = self.check_function_type_comment(node, newnode)
if type_comment_annotation:
type_comment_returns, type_comment_args = type_comment_annotation
newnode.postinit(
Expand Down
13 changes: 13 additions & 0 deletions tests/unittest_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,19 @@ def f_arg_comment(
assert actual_arg.as_string() == expected_arg


@pytest.mark.skipif(not HAS_TYPED_AST, reason="requires typed_ast")
def test_correct_function_type_comment_parent():
data = """
def f(a):
# type: (A) -> A
pass
"""
astroid = builder.parse(data)
f = astroid.body[0]
assert f.type_comment_args[0].parent is f
assert f.type_comment_returns.parent is f


def test_is_generator_for_yield_assignments():
node = astroid.extract_node(
"""
Expand Down