From 6dba72cde6171bc1d1a4131a81a209ac9bfdf76b Mon Sep 17 00:00:00 2001 From: temyurchenko <44875844+temyurchenko@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:13:50 -0400 Subject: [PATCH] Enforce a non-None parent in build_function (#2562) It's a part of the campaign to get rid of non-module roots --- astroid/raw_building.py | 6 ++++-- tests/test_raw_building.py | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/astroid/raw_building.py b/astroid/raw_building.py index b4e812f386..2d46706745 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -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, @@ -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, ) @@ -321,6 +322,7 @@ def object_build_function( return build_function( getattr(member, "__name__", ""), + node, args, posonlyargs, defaults, @@ -344,7 +346,7 @@ def object_build_methoddescriptor( """create astroid for a living method descriptor object""" # FIXME get arguments ? name = getattr(member, "__name__", "") - func = build_function(name, doc=member.__doc__) + func = build_function(name, node, doc=member.__doc__) _add_dunder_class(func, node, member) return func diff --git a/tests/test_raw_building.py b/tests/test_raw_building.py index 1325dbc3ee..b5f3a62623 100644 --- a/tests/test_raw_building.py +++ b/tests/test_raw_building.py @@ -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"