Skip to content

Commit

Permalink
Fix handling of name collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdanal committed Feb 18, 2024
1 parent 8e449f6 commit cdbde86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ def add_name(self, fullname: str, require: bool = True) -> str:
"""
module, name = fullname.rsplit(".", 1)
alias = "_" + name if name in self.defined_names else None
while alias in self.defined_names:
alias = "_" + alias
if module != "builtins" or alias: # don't import from builtins unless needed
self.import_tracker.add_import_from(module, [(name, alias)], require=require)
return alias or name
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -4292,6 +4292,21 @@ z_alias = _dict[str, float]
v_alias = _set[int]
w_alias = _list[_dict[str, _tuple[int, ...]]]

[case testHandlingNameCollisions]
# flags: --include-private
from typing import Tuple
tuple = int
_tuple = range
__tuple = map
x: Tuple[int, str]
[out]
from builtins import tuple as ___tuple

tuple = int
_tuple = range
__tuple = map
x: ___tuple[int, str]

[case testPEP570PosOnlyParams]
def f(x=0, /): ...
def f1(x: int, /): ...
Expand Down

0 comments on commit cdbde86

Please sign in to comment.