Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix finding packages without an __init__.py #1333

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
============================
Expand Down
8 changes: 7 additions & 1 deletion astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) :]
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/unittest_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import tempfile
import unittest
import xml
from pathlib import Path
from xml import etree
from xml.etree import ElementTree

Expand Down Expand Up @@ -189,6 +190,30 @@ def test_load_from_module_symlink_on_symlinked_paths_in_syspath(self) -> None:
# 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:
Expand Down