Issue 16094: Tuple extraction in a lambda isn't supported by 2to3 (original) (raw)

Created on 2012-09-30 11:07 by GlitchMr, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
lambda.py GlitchMr,2012-09-30 11:07 Testcase
lambda_tuple_params.py serhiy.storchaka,2012-09-30 13:20
Messages (8)
msg171627 - (view) Author: (GlitchMr) Date: 2012-09-30 11:07
Tuple extraction in lambda isn't supported with more than one argument.
msg171634 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2012-09-30 13:03
Python 2.7 and earlier are in bug-fix mode (means no new features) Tuple unpacking is not there in Python 3. Please close this bug as invalid.
msg171635 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-09-30 13:08
Works for me (Python 2.7.3): >>> x = lambda (a, b), c: a + b + c >>> x((2, 3), 4) 9 What issue are you seeing?
msg171636 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-09-30 13:20
The issue is in lib2to3. tuple_params does not work with lambdas. I confirm the bug. $ ./python -m lib2to3 -f tuple_params lambda_tuple_params.py RefactoringTool: Refactored lambda_tuple_params.py --- lambda_tuple_params.py (original) +++ lambda_tuple_params.py (refactored) @@ -1,3 +1,3 @@ lambda (a, b), c: a + b + c -def f((a, b), c): return a + b + c +def f(xxx_todo_changeme, c): (a, b) = xxx_todo_changeme; return a + b + c RefactoringTool: Files that need to be modified: RefactoringTool: lambda_tuple_params.py
msg171768 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-10-02 01:38
I'm not sure a semantically neutral automatic fix is possible: f = lambda (a, b), c: a + b + c # Py2.x f = lambda t, c: t[0] + t[1] + c # Py3.x The former will unpack any iterable, not just sequences: >>> def g(): yield 'a' yield 'b' >>> f(g(), 'c') 'abc' Also, the former will validate the number of arguments: >>> f((1,2,3), 4) Traceback (most recent call last): File "<pyshell#11>", line 1, in f((1,2,3), 4) File "<pyshell#0>", line 1, in f = lambda (a, b), c: a + b + c ValueError: too many values to unpack I don't see a way to automatically include those capabilities in an automatic 2-to-3 transformation.
msg171779 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-02 09:06
f = lambda t, c, *, _f=(lambda a, b, c: a + b + c): _f(*(unpack_tuple(2, t) + (c,))) def unpack_tuple(n, t): t = tuple(t) if len(t) > n: raise ValueError('too many values to unpack (expected %d)' % (n,)) if len(t) < n: raise ValueError('need more than %d values to unpack' % (len(t),)) return t
msg171846 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-10-03 01:37
Not pretty :-) IMO, we're better off leaving 2-to-3 without an automated conversion for this and people will just have to stop using the feature in their 2.7 code. We could add a -3 warning in Py2.7 to flag code that needs to be changed.
msg379319 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-22 17:58
Updating the versions, but from the discussion I think this can probably be closed as "won't fix".
History
Date User Action Args
2022-04-11 14:57:36 admin set github: 60298
2020-10-23 18:01:21 corona10 set status: open -> closedresolution: wont fixstage: needs patch -> resolved
2020-10-22 17:58:00 iritkatriel set nosy: + iritkatrielmessages: + versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.2, Python 3.3, Python 3.4
2012-10-03 01:37:30 rhettinger set messages: +
2012-10-02 09:06:22 serhiy.storchaka set messages: +
2012-10-02 01:38:27 rhettinger set nosy: + rhettingermessages: +
2012-09-30 16:47:00 ezio.melotti set nosy: + ezio.melotti
2012-09-30 16:41:03 brett.cannon set title: Tuple extraction in lambda isn't supported with more than one argument -> Tuple extraction in a lambda isn't supported by 2to3
2012-09-30 13:20:37 serhiy.storchaka set files: + lambda_tuple_params.pytype: enhancement -> behaviorversions: + Python 3.2, Python 3.3, Python 3.4nosy: + serhiy.storchaka, benjamin.petersonmessages: + stage: needs patch
2012-09-30 13:08:56 mark.dickinson set nosy: + mark.dickinsonmessages: +
2012-09-30 13:03:24 Ramchandra Apte set type: enhancementmessages: + nosy: + Ramchandra Apte
2012-09-30 11:07:12 GlitchMr create