|
18 | 18 | logger = logging.getLogger(__name__) |
19 | 19 |
|
20 | 20 |
|
21 | | -def _shared_leading_path(*paths): |
22 | | - """Identify the common leading parts between import paths. |
| 21 | +def _shared_leading_qualname(*qualnames): |
| 22 | + """Identify the common leading parts between fully qualified names. |
23 | 23 |
|
24 | 24 | Parameters |
25 | 25 | ---------- |
26 | | - *paths : tuple[str] |
| 26 | + *qualnames : tuple[str] |
27 | 27 |
|
28 | 28 | Returns |
29 | 29 | ------- |
30 | 30 | shared : str |
| 31 | + The names, still split by ".", that are common to the start of all given |
| 32 | + `qualnames`. Empty string if nothing is common. |
| 33 | +
|
| 34 | + Examples |
| 35 | + -------- |
| 36 | + >>> _shared_leading_qualname("foo.bar", "foo.baz") |
| 37 | + 'foo' |
| 38 | + >>> _shared_leading_qualname("foo.bar", "faa.baz") |
| 39 | + '' |
31 | 40 | """ |
32 | | - if len(paths) < 2: |
| 41 | + if len(qualnames) < 2: |
33 | 42 | raise ValueError("need more than two paths") |
34 | | - splits = (p.split(".") for p in paths) |
| 43 | + splits = (p.split(".") for p in qualnames) |
35 | 44 | shared = [] |
36 | | - for paths in zip(*splits, strict=False): |
37 | | - if all(paths[0] == p for p in paths): |
38 | | - shared.append(paths[0]) |
| 45 | + for names in zip(*splits, strict=False): |
| 46 | + if all(names[0] == p for p in names): |
| 47 | + shared.append(names[0]) |
39 | 48 | else: |
40 | 49 | break |
41 | 50 | return ".".join(shared) |
@@ -147,7 +156,7 @@ def format_import(self, relative_to=None): |
147 | 156 | import_path = self.import_path |
148 | 157 | if import_path: |
149 | 158 | if relative_to: |
150 | | - shared = _shared_leading_path(relative_to, import_path) |
| 159 | + shared = _shared_leading_qualname(relative_to, import_path) |
151 | 160 | if shared == import_path: |
152 | 161 | import_path = "." |
153 | 162 | else: |
|
0 commit comments