From 2e411a0d06b45fe84e86b1fbc77728c06732cc23 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 30 May 2022 21:43:21 -0400 Subject: [PATCH] Special case `__main__` in `is_namespace()` --- astroid/interpreter/_import/util.py | 4 ++-- tests/unittest_manager.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py index 3de921b889..5db1702d5a 100644 --- a/astroid/interpreter/_import/util.py +++ b/astroid/interpreter/_import/util.py @@ -34,8 +34,8 @@ def is_namespace(modname: str) -> bool: working_modname, path=last_submodule_search_locations ) except ValueError: - # executed .pth files may not have __spec__ - return True + # Assume it's a .pth file, unless it's __main__ + return modname != "__main__" except KeyError: # Intermediate steps might raise KeyErrors # https://github.com/python/cpython/issues/93334 diff --git a/tests/unittest_manager.py b/tests/unittest_manager.py index 922b78a9bf..e9a1957e44 100644 --- a/tests/unittest_manager.py +++ b/tests/unittest_manager.py @@ -127,6 +127,7 @@ def test_submodule_homonym_with_non_module(self) -> None: def test_module_is_not_namespace(self) -> None: self.assertFalse(util.is_namespace("tests.testdata.python3.data.all")) + self.assertFalse(util.is_namespace("__main__")) def test_implicit_namespace_package(self) -> None: data_dir = os.path.dirname(resources.find("data/namespace_pep_420"))