@@ -789,14 +789,15 @@ def resolve_pkg_root_and_module_name(
789
789
start = pkg_root if pkg_root is not None else path .parent
790
790
for candidate in (start , * start .parents ):
791
791
module_name = compute_module_name (candidate , path )
792
- if is_importable (module_name , path ):
792
+ if module_name and is_importable (module_name , path ):
793
793
# Point the pkg_root to the root of the namespace package.
794
794
pkg_root = candidate
795
795
break
796
796
797
797
if pkg_root is not None :
798
798
module_name = compute_module_name (pkg_root , path )
799
- return pkg_root , module_name
799
+ if module_name :
800
+ return pkg_root , module_name
800
801
801
802
raise CouldNotResolvePathError (f"Could not resolve for { path } " )
802
803
@@ -827,16 +828,22 @@ def is_importable(module_name: str, module_path: Path) -> bool:
827
828
return spec_matches_module_path (spec , module_path )
828
829
829
830
830
- def compute_module_name (root : Path , module_path : Path ) -> str :
831
+ def compute_module_name (root : Path , module_path : Path ) -> Optional [ str ] :
831
832
"""Compute a module name based on a path and a root anchor."""
832
833
try :
833
834
path_without_suffix = module_path .with_suffix ("" )
834
835
except ValueError :
835
836
# Empty paths (such as Path.cwd()) might break meta_path hooks (like our own assertion rewriter).
836
- return ""
837
+ return None
837
838
838
- names = list (path_without_suffix .relative_to (root ).parts )
839
- if names and names [- 1 ] == "__init__" :
839
+ try :
840
+ relative = path_without_suffix .relative_to (root )
841
+ except ValueError : # pragma: no cover
842
+ return None
843
+ names = list (relative .parts )
844
+ if not names :
845
+ return None
846
+ if names [- 1 ] == "__init__" :
840
847
names .pop ()
841
848
return "." .join (names )
842
849
0 commit comments