summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-01-09 16:57:55 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-01-09 11:18:18 +0100
commita03c07d835d138086160fa2452b7b7297f41ed9b (patch)
tree86bdf611ccea79dd4f686b8498cd3a58b9215a1f
parent68c78e5c8d799fee964be84f8eda9d2207cb5543 (diff)
downloadastroid-git-a03c07d835d138086160fa2452b7b7297f41ed9b.tar.gz
Fix finding packages without an ``__init__.py`` (#1333)
-rw-r--r--ChangeLog8
-rw-r--r--astroid/modutils.py8
-rw-r--r--tests/unittest_modutils.py25
3 files changed, 40 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 6c56d234..783391c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,14 @@ What's New in astroid 2.10.0?
Release date: TBA
+What's New in astroid 2.9.3?
+============================
+Release date: TBA
+
+* Fixed regression where packages without a ``__init__.py`` file were
+ not recognized or imported correctly.
+
+ Closes #1327
What's New in astroid 2.9.2?
============================
diff --git a/astroid/modutils.py b/astroid/modutils.py
index 6d698469..b20a1840 100644
--- a/astroid/modutils.py
+++ b/astroid/modutils.py
@@ -297,6 +297,9 @@ def _get_relative_base_path(filename, path_to_check):
if os.path.normcase(real_filename).startswith(path_to_check):
importable_path = real_filename
+ # if "var" in path_to_check:
+ # breakpoint()
+
if importable_path:
base_path = os.path.splitext(importable_path)[0]
relative_base_path = base_path[len(path_to_check) :]
@@ -307,8 +310,11 @@ def _get_relative_base_path(filename, path_to_check):
def modpath_from_file_with_callback(filename, path=None, is_package_cb=None):
filename = os.path.expanduser(_path_from_filename(filename))
+ paths_to_check = sys.path.copy()
+ if path:
+ paths_to_check += path
for pathname in itertools.chain(
- path or [], map(_cache_normalize_path, sys.path), sys.path
+ paths_to_check, map(_cache_normalize_path, paths_to_check)
):
if not pathname:
continue
diff --git a/tests/unittest_modutils.py b/tests/unittest_modutils.py
index d6fcb68e..01d5e5b9 100644
--- a/tests/unittest_modutils.py
+++ b/tests/unittest_modutils.py
@@ -30,6 +30,7 @@ import sys
import tempfile
import unittest
import xml
+from pathlib import Path
from xml import etree
from xml.etree import ElementTree
@@ -189,6 +190,30 @@ class ModPathFromFileTest(unittest.TestCase):
# this should be equivalent to: import secret
self.assertEqual(modutils.modpath_from_file(symlink_secret_path), ["secret"])
+ def test_load_packages_without_init(self) -> None:
+ """Test that we correctly find packages with an __init__.py file.
+
+ Regression test for issue reported in:
+ https://github.com/PyCQA/astroid/issues/1327
+ """
+ tmp_dir = Path(tempfile.gettempdir())
+ self.addCleanup(os.chdir, os.curdir)
+ os.chdir(tmp_dir)
+
+ self.addCleanup(shutil.rmtree, tmp_dir / "src")
+ os.mkdir(tmp_dir / "src")
+ os.mkdir(tmp_dir / "src" / "package")
+ with open(tmp_dir / "src" / "__init__.py", "w", encoding="utf-8"):
+ pass
+ with open(tmp_dir / "src" / "package" / "file.py", "w", encoding="utf-8"):
+ pass
+
+ # this should be equivalent to: import secret
+ self.assertEqual(
+ modutils.modpath_from_file(str(Path("src") / "package"), ["."]),
+ ["src", "package"],
+ )
+
class LoadModuleFromPathTest(resources.SysPathSetup, unittest.TestCase):
def test_do_not_load_twice(self) -> None: