Closes Issue 21659: Improve Idle calltips for *args, **kwargs in 2.7, where actual
names are not available. Initial patch by Serhiy Storchaka.
diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/CallTips.py
index 0662880..3db2636 100644
--- a/Lib/idlelib/CallTips.py
+++ b/Lib/idlelib/CallTips.py
@@ -183,10 +183,16 @@
         defaults = list(map(lambda name: "=%s" % repr(name), defaults))
         defaults = [""] * (len(real_args) - len(defaults)) + defaults
         items = map(lambda arg, dflt: arg + dflt, real_args, defaults)
-        if fob.func_code.co_flags & 0x4:
-            items.append("*args")
-        if fob.func_code.co_flags & 0x8:
-            items.append("**kwds")
+        for flag, pre, name in ((0x4, '*', 'args'), (0x8, '**', 'kwargs')):
+            if fob.func_code.co_flags & flag:
+                pre_name = pre + name
+                if name not in real_args:
+                    items.append(pre_name)
+                else:
+                    i = 1
+                    while ((name+'%s') % i) in real_args:
+                        i += 1
+                    items.append((pre_name+'%s') % i)
         argspec = ", ".join(items)
         argspec = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", argspec)
 
diff --git a/Lib/idlelib/idle_test/test_calltips.py b/Lib/idlelib/idle_test/test_calltips.py
index 60b688f..8371809 100644
--- a/Lib/idlelib/idle_test/test_calltips.py
+++ b/Lib/idlelib/idle_test/test_calltips.py
@@ -22,7 +22,7 @@
     def t4(self, *args): 'doc'
     t4.tip = "(self, *args)"
     def t5(self, ai, b=None, *args, **kw): 'doc'
-    t5.tip = "(self, ai, b=None, *args, **kwds)"
+    t5.tip = "(self, ai, b=None, *args, **kwargs)"
     def t6(no, self): 'doc'
     t6.tip = "(no, self)"
     def __call__(self, ci): 'doc'
@@ -104,7 +104,7 @@
         def t4(*args): 'doc'
         t4.tip = "(*args)"
         def t5(a, b=None, *args, **kwds): 'doc'
-        t5.tip = "(a, b=None, *args, **kwds)"
+        t5.tip = "(a, b=None, *args, **kwargs)"
 
         for func in (t1, t2, t3, t4, t5, TC):
             self.assertEqual(signature(func), func.tip + '\ndoc')
@@ -126,10 +126,16 @@
         class C:
             def m1(*args): pass
             def m2(**kwds): pass
+        def f1(args, kwargs, *a, **k): pass
+        def f2(args, kwargs, args1, kwargs1, *a, **k): pass
         c = C()
-        for meth, mtip  in ((C.m1, '(*args)'), (c.m1, "(*args)"),
-                                      (C.m2, "(**kwds)"), (c.m2, "(**kwds)"),):
-            self.assertEqual(signature(meth), mtip)
+        self.assertEqual(signature(C.m1), '(*args)')
+        self.assertEqual(signature(c.m1), '(*args)')
+        self.assertEqual(signature(C.m2), '(**kwargs)')
+        self.assertEqual(signature(c.m2), '(**kwargs)')
+        self.assertEqual(signature(f1), '(args, kwargs, *args1, **kwargs1)')
+        self.assertEqual(signature(f2),
+                         '(args, kwargs, args1, kwargs1, *args2, **kwargs2)')
 
     def test_no_docstring(self):
         def nd(s): pass