Skip to content

Commit cdbde86

Browse files
committed
Fix handling of name collisions
1 parent 8e449f6 commit cdbde86

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mypy/stubutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ def add_name(self, fullname: str, require: bool = True) -> str:
635635
"""
636636
module, name = fullname.rsplit(".", 1)
637637
alias = "_" + name if name in self.defined_names else None
638+
while alias in self.defined_names:
639+
alias = "_" + alias
638640
if module != "builtins" or alias: # don't import from builtins unless needed
639641
self.import_tracker.add_import_from(module, [(name, alias)], require=require)
640642
return alias or name

test-data/unit/stubgen.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4292,6 +4292,21 @@ z_alias = _dict[str, float]
42924292
v_alias = _set[int]
42934293
w_alias = _list[_dict[str, _tuple[int, ...]]]
42944294

4295+
[case testHandlingNameCollisions]
4296+
# flags: --include-private
4297+
from typing import Tuple
4298+
tuple = int
4299+
_tuple = range
4300+
__tuple = map
4301+
x: Tuple[int, str]
4302+
[out]
4303+
from builtins import tuple as ___tuple
4304+
4305+
tuple = int
4306+
_tuple = range
4307+
__tuple = map
4308+
x: ___tuple[int, str]
4309+
42954310
[case testPEP570PosOnlyParams]
42964311
def f(x=0, /): ...
42974312
def f1(x: int, /): ...

0 commit comments

Comments
 (0)