From 02fa714ab4b6307a85da7d8e7566a89b0f4987d1 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Tue, 3 Sep 2024 16:12:12 -0700 Subject: [PATCH] add the parent node to "__main__" Not having a parent leads to weird situations, like `root()` returning the node itself, not a `Module`. --- astroid/nodes/scoped_nodes/scoped_nodes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index af3b9d39a..2f7e8001b 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -354,7 +354,9 @@ def getattr( if name in self.special_attributes and not ignore_locals and not name_in_locals: result = [self.special_attributes.lookup(name)] if name == "__name__": - result.append(const_factory("__main__")) + main_const = const_factory("__main__") + main_const.parent = AstroidManager().builtins_module + result.append(main_const) elif not ignore_locals and name_in_locals: result = self.locals[name] elif self.package: