From 0011e7ffde2054d2b1c9e3b0de3689d11a706255 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 d79223d29b..5aed80869e 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: