summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2022-05-31 04:32:00 -0400
committerGitHub <noreply@github.com>2022-05-31 10:32:00 +0200
commit1ccb1c455b71bcf453253d996e3739f5153f2774 (patch)
treea132b4ec908d80a95cabcd81bdea31b40d1d214d
parent2ff48000b6896e17ffe7ca04722cf6a61b7a27cb (diff)
downloadastroid-git-1ccb1c455b71bcf453253d996e3739f5153f2774.tar.gz
Special case `__main__` in `is_namespace()` (#1579)
-rw-r--r--astroid/interpreter/_import/util.py4
-rw-r--r--tests/unittest_manager.py1
2 files changed, 3 insertions, 2 deletions
diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py
index 3de921b8..5db1702d 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 922b78a9..e9a1957e 100644
--- a/tests/unittest_manager.py
+++ b/tests/unittest_manager.py
@@ -127,6 +127,7 @@ class AstroidManagerTest(
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"))