diff --git a/ChangeLog b/ChangeLog index 0e4312e57..01182a975 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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? diff --git a/astroid/modutils.py b/astroid/modutils.py index 0868c60c0..bb2c7c4dd 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -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: diff --git a/tests/test_modutils.py b/tests/test_modutils.py index 1d6959f11..f50c53590 100644 --- a/tests/test_modutils.py +++ b/tests/test_modutils.py @@ -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."""