Compute a module name based on a path and a root anchor.
(root: Path, module_path: Path)
| 915 | |
| 916 | |
| 917 | def compute_module_name(root: Path, module_path: Path) -> str | None: |
| 918 | """Compute a module name based on a path and a root anchor.""" |
| 919 | try: |
| 920 | path_without_suffix = module_path.with_suffix("") |
| 921 | except ValueError: |
| 922 | # Empty paths (such as Path.cwd()) might break meta_path hooks (like our own assertion rewriter). |
| 923 | return None |
| 924 | |
| 925 | try: |
| 926 | relative = path_without_suffix.relative_to(root) |
| 927 | except ValueError: # pragma: no cover |
| 928 | return None |
| 929 | names = list(relative.parts) |
| 930 | if not names: |
| 931 | return None |
| 932 | if names[-1] == "__init__": |
| 933 | names.pop() |
| 934 | return ".".join(names) |
| 935 | |
| 936 | |
| 937 | class CouldNotResolvePathError(Exception): |