homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argument names in __annotations__ are not mangled for functions defined inside class scope
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: Arfrever, benjamin.peterson, eric.araujo, jonasw, larry, ncoghlan, nnorwitz, python-dev, yselivanov
Priority: high Keywords: patch

Created on 2014年02月14日 11:57 by jonasw, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue_20625_01.patch yselivanov, 2014年02月14日 19:17 review
issue_20625_02.patch yselivanov, 2014年02月14日 22:59 review
Messages (12)
msg211215 - (view) Author: Jonas Wielicki (jonasw) Date: 2014年02月14日 11:57
Assume I have this code:
class Spam:
 def eggs(__some_kwarg:int=None):
 print(__some_kwarg)
I can call Spam.bar with keyword arguments as expected from mangling:
>>> Spam.eggs(10)
10
>>> Spam.eggs(_Spam__some_kwarg=10)
10
However, in the __annotations__ field, the argument name is not mangled:
>>> Spam.eggs.__annotations__
{'__some_kwarg': <class 'int'>}
This is an inconsistency which makes it difficult to work with function annotations in this case.
msg211231 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014年02月14日 18:31
It looks like it's a bug.
Spam.eggs.__code__.co_varnames will have '_Spam__some_kwarg' as a name of the first parameter. So __annotations__ should have '_Spam__some_kwarg' instead of '__some_kwarg'.
msg211234 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014年02月14日 18:35
Furthermore:
class Foo:
 def bar(self, *, __kw:'anno'='default'):
 pass
>>> Foo.bar.__annotations__
{'__kw': 'anno'}
>>> Foo.bar.__kwdefaults__
{'_Foo__kw': 'default'}
msg211237 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014年02月14日 19:17
Patch is attached. Please review.
msg211242 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014年02月14日 22:22
Why would we mangle the names of arguments in the first place? What possible use case is there?
msg211243 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014年02月14日 22:34
> Why would we mangle the names of arguments in the first place? What possible use case is there?
The use case is multiple inheritance. You can define your methods with keyword and keyword-only params starting with '__', put in them some cached default for speeding up lookup time, and not being worried that someone can override the arg. The second reason is, I believe, is desire to be consistent with mangling methods and attributes.
Again, using mangling for function arguments is, probably, not the best practice, and that, combined with the fact, that people are just starting to play with annotations, explains why this bug was so long unnoticed.
But the semantics of arg names mangling is already there, implemented everywhere but __annotations__.
The attached patch is fairly simple, and really is just a bug fix. It would be great if we can get this in 3.4 (and even in 3.3 and 3.2).
msg211244 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014年02月14日 22:43
(see review on rietveld)
msg211248 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014年02月14日 22:59
lgtm
msg211531 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014年02月18日 17:50
New changeset a63327162063 by Yury Selivanov in branch 'default':
Mangle __parameters in __annotations__ dict properly. Issue #20625.
http://hg.python.org/cpython/rev/a63327162063 
msg211556 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014年02月18日 21:08
New changeset 5202aca8a673 by Victor Stinner in branch 'default':
Issue #20625: Fix compilation issue
http://hg.python.org/cpython/rev/5202aca8a673 
msg211671 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014年02月19日 23:07
New changeset e301a515f8f4 by Benjamin Peterson in branch 'default':
update magic number for #20625
http://hg.python.org/cpython/rev/e301a515f8f4 
msg213822 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014年03月17日 06:31
New changeset 4d7c3cbd8515 by Yury Selivanov in branch '3.4':
Mangle __parameters in __annotations__ dict properly. Issue #20625.
http://hg.python.org/cpython/rev/4d7c3cbd8515
New changeset 3bced76d2706 by Victor Stinner in branch '3.4':
Issue #20625: Fix compilation issue
http://hg.python.org/cpython/rev/3bced76d2706
New changeset e5cde3fd7c74 by Benjamin Peterson in branch '3.4':
update magic number for #20625
http://hg.python.org/cpython/rev/e5cde3fd7c74 
History
Date User Action Args
2022年04月11日 14:57:58adminsetgithub: 64824
2014年03月17日 06:31:02python-devsetmessages: + msg213822
2014年02月20日 03:41:00Arfreversetnosy: + Arfrever
2014年02月19日 23:07:18python-devsetmessages: + msg211671
2014年02月18日 21:08:12python-devsetmessages: + msg211556
2014年02月18日 17:56:03yselivanovsetstatus: open -> closed
assignee: yselivanov
resolution: fixed
2014年02月18日 17:50:07python-devsetnosy: + python-dev
messages: + msg211531
2014年02月14日 22:59:56benjamin.petersonsetmessages: + msg211248
2014年02月14日 22:59:02yselivanovsetfiles: + issue_20625_02.patch
2014年02月14日 22:43:10benjamin.petersonsetmessages: + msg211244
2014年02月14日 22:34:40yselivanovsetmessages: + msg211243
2014年02月14日 22:22:51larrysetmessages: + msg211242
2014年02月14日 19:19:25yselivanovsetpriority: normal -> high
2014年02月14日 19:18:29yselivanovsetversions: + Python 3.4
2014年02月14日 19:17:14yselivanovsetfiles: + issue_20625_01.patch

nosy: + nnorwitz, benjamin.peterson
messages: + msg211237

keywords: + patch
2014年02月14日 18:35:39yselivanovsetmessages: + msg211234
2014年02月14日 18:31:14yselivanovsetmessages: + msg211231
2014年02月14日 18:21:00eric.araujosetnosy: + eric.araujo
2014年02月14日 15:58:21yselivanovsetnosy: + ncoghlan, larry, yselivanov
2014年02月14日 11:57:28jonaswcreate

AltStyle によって変換されたページ (->オリジナル) /