Skip to content

Commit

Permalink
[mypyc] fix name generation for modules with similar full names (pyth…
Browse files Browse the repository at this point in the history
…on#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.
  • Loading branch information
aatle authored Oct 21, 2024
1 parent cb9dd7d commit 5bae05d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mypyc/namegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ def make_module_translation_map(names: list[str]) -> dict[str, str]:
for name in names:
for suffix in candidate_suffixes(name):
if num_instances[suffix] == 1:
result[name] = suffix
break
else:
assert False, names
# Takes the last suffix if none are unique
result[name] = suffix
return result


Expand Down
6 changes: 6 additions & 0 deletions mypyc/test/test_namegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def test_make_module_translation_map(self) -> None:
"fu.bar": "fu.bar.",
"foo.baz": "baz.",
}
assert make_module_translation_map(["foo", "foo.foo", "bar.foo", "bar.foo.bar.foo"]) == {
"foo": "foo.",
"foo.foo": "foo.foo.",
"bar.foo": "bar.foo.",
"bar.foo.bar.foo": "foo.bar.foo.",
}

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

0 comments on commit 5bae05d

Please sign in to comment.