classification
Title: Tuple extraction in a lambda isn't supported by 2to3
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: GlitchMr, Ramchandra Apte, benjamin.peterson, ezio.melotti, iritkatriel, mark.dickinson, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2012-09-30 11:07 by GlitchMr, last changed 2020-10-23 18:01 by corona10. 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 <module>
        f((1,2,3), 4)
      File "<pyshell#0>", line 1, in <lambda>
        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
2020-10-23 18:01:21corona10setstatus: open -> closed
resolution: wont fix
stage: needs patch -> resolved
2020-10-22 17:58:00iritkatrielsetnosy: + iritkatriel

messages: + msg379319
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:30rhettingersetmessages: + msg171846
2012-10-02 09:06:22serhiy.storchakasetmessages: + msg171779
2012-10-02 01:38:27rhettingersetnosy: + rhettinger
messages: + msg171768
2012-09-30 16:47:00ezio.melottisetnosy: + ezio.melotti
2012-09-30 16:41:03brett.cannonsettitle: 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:37serhiy.storchakasetfiles: + lambda_tuple_params.py

type: enhancement -> behavior
versions: + Python 3.2, Python 3.3, Python 3.4
nosy: + serhiy.storchaka, benjamin.peterson

messages: + msg171636
stage: needs patch
2012-09-30 13:08:56mark.dickinsonsetnosy: + mark.dickinson
messages: + msg171635
2012-09-30 13:03:24Ramchandra Aptesettype: enhancement

messages: + msg171634
nosy: + Ramchandra Apte
2012-09-30 11:07:12GlitchMrcreate