Skip to content

Commit a03c07d

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Fix finding packages without an __init__.py (#1333)
1 parent 68c78e5 commit a03c07d

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ What's New in astroid 2.10.0?
77
Release date: TBA
88

99

10+
What's New in astroid 2.9.3?
11+
============================
12+
Release date: TBA
13+
14+
* Fixed regression where packages without a ``__init__.py`` file were
15+
not recognized or imported correctly.
16+
17+
Closes #1327
1018

1119
What's New in astroid 2.9.2?
1220
============================

astroid/modutils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ def _get_relative_base_path(filename, path_to_check):
297297
if os.path.normcase(real_filename).startswith(path_to_check):
298298
importable_path = real_filename
299299

300+
# if "var" in path_to_check:
301+
# breakpoint()
302+
300303
if importable_path:
301304
base_path = os.path.splitext(importable_path)[0]
302305
relative_base_path = base_path[len(path_to_check) :]
@@ -307,8 +310,11 @@ def _get_relative_base_path(filename, path_to_check):
307310

308311
def modpath_from_file_with_callback(filename, path=None, is_package_cb=None):
309312
filename = os.path.expanduser(_path_from_filename(filename))
313+
paths_to_check = sys.path.copy()
314+
if path:
315+
paths_to_check += path
310316
for pathname in itertools.chain(
311-
path or [], map(_cache_normalize_path, sys.path), sys.path
317+
paths_to_check, map(_cache_normalize_path, paths_to_check)
312318
):
313319
if not pathname:
314320
continue

tests/unittest_modutils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import tempfile
3131
import unittest
3232
import xml
33+
from pathlib import Path
3334
from xml import etree
3435
from xml.etree import ElementTree
3536

@@ -189,6 +190,30 @@ def test_load_from_module_symlink_on_symlinked_paths_in_syspath(self) -> None:
189190
# this should be equivalent to: import secret
190191
self.assertEqual(modutils.modpath_from_file(symlink_secret_path), ["secret"])
191192

193+
def test_load_packages_without_init(self) -> None:
194+
"""Test that we correctly find packages with an __init__.py file.
195+
196+
Regression test for issue reported in:
197+
https://github.com/PyCQA/astroid/issues/1327
198+
"""
199+
tmp_dir = Path(tempfile.gettempdir())
200+
self.addCleanup(os.chdir, os.curdir)
201+
os.chdir(tmp_dir)
202+
203+
self.addCleanup(shutil.rmtree, tmp_dir / "src")
204+
os.mkdir(tmp_dir / "src")
205+
os.mkdir(tmp_dir / "src" / "package")
206+
with open(tmp_dir / "src" / "__init__.py", "w", encoding="utf-8"):
207+
pass
208+
with open(tmp_dir / "src" / "package" / "file.py", "w", encoding="utf-8"):
209+
pass
210+
211+
# this should be equivalent to: import secret
212+
self.assertEqual(
213+
modutils.modpath_from_file(str(Path("src") / "package"), ["."]),
214+
["src", "package"],
215+
)
216+
192217

193218
class LoadModuleFromPathTest(resources.SysPathSetup, unittest.TestCase):
194219
def test_do_not_load_twice(self) -> None:

0 commit comments

Comments
 (0)