Skip to content

Commit 5bae05d

Browse files
authored
[mypyc] fix name generation for modules with similar full names (#18001)
Fixes mypyc/mypyc#1071 Adds a test to cover this case Building certain package layouts now succeeds instead of failing. The behavior for all package layouts not affected by the error is unchanged. In `namegen.make_module_translation_map(names)`, if argument `names` have `"foo"` and `"foo.foo"`, all suffixes found for `"foo"` are also found for `"foo.foo"`. This means that module `foo` has no unique suffixes, which currently causes an `AssertionError`. The fix forces a module to take the last, fullest suffix if none are unique. It is guaranteed that no other module will also take the same suffix because they either will have a unique suffix to take, or they will take the fullest suffix for their name which is always going to be different.
1 parent cb9dd7d commit 5bae05d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

mypyc/namegen.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ def make_module_translation_map(names: list[str]) -> dict[str, str]:
100100
for name in names:
101101
for suffix in candidate_suffixes(name):
102102
if num_instances[suffix] == 1:
103-
result[name] = suffix
104103
break
105-
else:
106-
assert False, names
104+
# Takes the last suffix if none are unique
105+
result[name] = suffix
107106
return result
108107

109108

mypyc/test/test_namegen.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def test_make_module_translation_map(self) -> None:
3535
"fu.bar": "fu.bar.",
3636
"foo.baz": "baz.",
3737
}
38+
assert make_module_translation_map(["foo", "foo.foo", "bar.foo", "bar.foo.bar.foo"]) == {
39+
"foo": "foo.",
40+
"foo.foo": "foo.foo.",
41+
"bar.foo": "bar.foo.",
42+
"bar.foo.bar.foo": "foo.bar.foo.",
43+
}
3844

3945
def test_name_generator(self) -> None:
4046
g = NameGenerator([["foo", "foo.zar"]])

0 commit comments

Comments
 (0)