Mangle __parameters in __annotations__ dict properly. Issue #20625. · pythoncapi/cpython@026019f · GitHub
Skip to content

Commit 026019f

Browse files
author
Yury Selivanov
committed
Mangle __parameters in __annotations__ dict properly. Issue python#20625.
1 parent ee227ae commit 026019f

5 files changed

Lines changed: 37 additions & 1 deletion

File tree

Doc/whatsnew/3.4.rst

Lines changed: 4 additions & 0 deletions

Lib/test/test_grammar.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,13 @@ def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
314314
self.assertEqual(f.__annotations__,
315315
{'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
316316
'k': 11, 'return': 12})
317+
# Check for issue #20625 -- annotations mangling
318+
class Spam:
319+
def f(self, *, __kw:1):
320+
pass
321+
class Ham(Spam): pass
322+
self.assertEquals(Spam.f.__annotations__, {'_Spam__kw': 1})
323+
self.assertEquals(Ham.f.__annotations__, {'_Spam__kw': 1})
317324
# Check for SF Bug #1697248 - mixing decorators and a return annotation
318325
def null(x): return x
319326
@null

Lib/test/test_inspect.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,22 @@ def test() -> 42:
23902390
self.assertEqual(sig.return_annotation, 42)
23912391
self.assertEqual(sig, inspect.signature(test))
23922392

2393+
def test_signature_on_mangled_parameters(self):
2394+
class Spam:
2395+
def foo(self, __p1:1=2, *, __p2:2=3):
2396+
pass
2397+
class Ham(Spam):
2398+
pass
2399+
2400+
self.assertEqual(self.signature(Spam.foo),
2401+
((('self', ..., ..., "positional_or_keyword"),
2402+
('_Spam__p1', 2, 1, "positional_or_keyword"),
2403+
('_Spam__p2', 3, 2, "keyword_only")),
2404+
...))
2405+
2406+
self.assertEqual(self.signature(Spam.foo),
2407+
self.signature(Ham.foo))
2408+
23932409

23942410
class TestParameterObject(unittest.TestCase):
23952411
def test_signature_parameter_kinds(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: 2014-02-23
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #20625: Parameter names in __annotations__ were not mangled properly.
14+
Discovered by Jonas Wielicki, patch by Yury Selivanov.
15+
1316
- Issue #20261: In pickle, lookup __getnewargs__ and __getnewargs_ex__ on the
1417
type of the object.
1518

Python/compile.c

Lines changed: 7 additions & 1 deletion

0 commit comments

Comments
 (0)