Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ What's New in astroid 4.0.4?
============================
Release date: TBA

* Fix ability to detect .py modules inside PATH directories on Windows
described by a UNC path with a trailing backslash (`\`)
- Example: modutils.modpath_from_file(filename=r"\\Mac\Code\tests\test_resources.py", path=["\\mac\code\"]) == ['tests', 'test_resources']



What's New in astroid 4.0.3?
Expand Down
6 changes: 5 additions & 1 deletion astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ def _is_subpath(path: str, base: str) -> bool:
base = os.path.normcase(os.path.normpath(base))
if not path.startswith(base):
return False
return (len(path) == len(base)) or (path[len(base)] == os.path.sep)
return (
(len(path) == len(base))
or (path[len(base)] == os.path.sep)
or (base.endswith(os.path.sep) and path[len(base) - 1] == os.path.sep)
)


def _get_relative_base_path(filename: str, path_to_check: str) -> list[str] | None:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ def test_get_module_part_only_dot(self) -> None:
self.assertEqual(modutils.get_module_part(".", modutils.__file__), ".")


class IsSubpathTest(unittest.TestCase):
"""Tests for modutils._is_subpath,
which is used internally by modutils.modpath_from_file."""

@unittest.skipUnless(sys.platform == "win32", "Windows-specific path test")
def test_is_subpath_with_trailing_separator_unc_path(self) -> None:
self.assertTrue(
modutils._is_subpath(
"\\\\Mac\\Code\\tests\\test_resources.py",
# UNC path with trailing separator
"\\\\mac\\code\\",
)
)

@unittest.skipUnless(sys.platform == "win32", "Windows-specific path test")
def test_is_subpath_without_trailing_separator_unc_path(self) -> None:
self.assertTrue(
modutils._is_subpath(
"\\\\Mac\\Code\\tests\\test_resources.py",
# UNC path without trailing separator
"\\\\mac\\code",
)
)


class ModPathFromFileTest(unittest.TestCase):
"""Given an absolute file path return the python module's path as a list."""

Expand Down