Skip to content

Commit

Permalink
Enforce a non-None parent in build_function (pylint-dev#2562)
Browse files Browse the repository at this point in the history
It's a part of the campaign to get rid of non-module roots
  • Loading branch information
temyurchenko authored Oct 2, 2024
1 parent e3813e3 commit 6dba72c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def build_class(

def build_function(
name: str,
parent: nodes.NodeNG,
args: list[str] | None = None,
posonlyargs: list[str] | None = None,
defaults: list[Any] | None = None,
Expand All @@ -149,7 +150,7 @@ def build_function(
name,
lineno=0,
col_offset=0,
parent=node_classes.Unknown(),
parent=parent,
end_col_offset=0,
end_lineno=0,
)
Expand Down Expand Up @@ -321,6 +322,7 @@ def object_build_function(

return build_function(
getattr(member, "__name__", "<no-name>"),
node,
args,
posonlyargs,
defaults,
Expand All @@ -344,7 +346,7 @@ def object_build_methoddescriptor(
"""create astroid for a living method descriptor object"""
# FIXME get arguments ?
name = getattr(member, "__name__", "<no-name>")
func = build_function(name, doc=member.__doc__)
func = build_function(name, node, doc=member.__doc__)
_add_dunder_class(func, node, member)
return func

Expand Down
10 changes: 5 additions & 5 deletions tests/test_raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@ def test_build_class(self) -> None:
self.assertEqual(node.doc_node, None)

def test_build_function(self) -> None:
node = build_function("MyFunction")
node = build_function("MyFunction", DUMMY_MOD)
self.assertEqual(node.name, "MyFunction")
self.assertEqual(node.doc_node, None)

def test_build_function_args(self) -> None:
args = ["myArgs1", "myArgs2"]
node = build_function("MyFunction", args)
node = build_function("MyFunction", DUMMY_MOD, args)
self.assertEqual("myArgs1", node.args.args[0].name)
self.assertEqual("myArgs2", node.args.args[1].name)
self.assertEqual(2, len(node.args.args))

def test_build_function_defaults(self) -> None:
defaults = ["defaults1", "defaults2"]
node = build_function(name="MyFunction", args=None, defaults=defaults)
node = build_function("MyFunction", DUMMY_MOD, args=None, defaults=defaults)
self.assertEqual(2, len(node.args.defaults))

def test_build_function_posonlyargs(self) -> None:
node = build_function(name="MyFunction", posonlyargs=["a", "b"])
node = build_function("MyFunction", DUMMY_MOD, posonlyargs=["a", "b"])
self.assertEqual(2, len(node.args.posonlyargs))

def test_build_function_kwonlyargs(self) -> None:
node = build_function(name="MyFunction", kwonlyargs=["a", "b"])
node = build_function("MyFunction", DUMMY_MOD, kwonlyargs=["a", "b"])
assert len(node.args.kwonlyargs) == 2
assert node.args.kwonlyargs[0].name == "a"
assert node.args.kwonlyargs[1].name == "b"
Expand Down

0 comments on commit 6dba72c

Please sign in to comment.