[Patches] [ python-Patches-1678060 ] Removal of Tuple Parameter Unpacking [PEP3113]

SourceForge.net noreply at sourceforge.net
Fri Mar 23 17:30:54 CET 2007


Patches item #1678060, was opened at 2007-03-10 16:34
Message generated for change (Comment added) made by jimjjewett
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1678060&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: Python 3000
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Tony Lownds (tonylownds)
Assigned to: Brett Cannon (bcannon)
Summary: Removal of Tuple Parameter Unpacking [PEP3113]

Initial Comment:
Remove tuple parameter unpacking. The Grammar is now:

typedargslist: ((tfpdef ['=' test] ',')*
                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
tfpdef: NAME [':' test]
varargslist: ((vfpdef ['=' test] ',')*
              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)
              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
vfpdef: NAME

Tuple parameters are removed from Lib/.

----------------------------------------------------------------------

Comment By: Jim Jewett (jimjjewett)
Date: 2007-03-23 12:30

Message:
Logged In: YES 
user_id=764593
Originator: NO

Brett,

2to3 is an important tool, but probably won't ever match good hand-work.

(1)  There are several places where he improved the code.

replacing map + an unpacking nested function with a generator:
"""
 def classify_class_attrs(object):
     """Wrap inspect.classify_class_attrs, with fixup for data
descriptors."""
-    def fixup((name, kind, cls, value)):
+    for (name, kind, cls, value) in
inspect.classify_class_attrs(object):
         if inspect.isdatadescriptor(value):
             kind = 'data descriptor'
-        return name, kind, cls, value
-    return map(fixup, inspect.classify_class_attrs(object))
+        yield name, kind, cls, value
"""


Replacing a paren-laden lambda with named function:
"""
-            items = sorted(items, key=lambda (k, v): (str(type(k)), k,
v))
+            def sortkey(item):
+                key, value = item
+                return str(type(key)), key, value
+            items = sorted(items, key=sortkey)
"""

Replacing filter + lambda + unpack with list comprehension:
"""
-        attrs = filter(lambda (name, kind, cls, value):
visiblename(name),
-                       classify_class_attrs(object))
+        attrs = [(name, kind, cls, value)
+                 for name, kind, cls, value in
classify_class_attrs(object)
+                 if visiblename(name)]
"""


(2)  His names are better.

I prefer his (name, result, expected)) over the current (name, (v1,v2),
(e1,e2))), let alone (name, _v1_v2_, _e1_e2_))

Even looking just at code with good existing names, he uses components
instead of _scheme_netloc_url_query_fragment_

So we get (what looks like human-readable source)

-def urlunsplit((scheme, netloc, url, query, fragment)):
+def urlunsplit(components):
+    (scheme, netloc, url, query, fragment) = components

instead of (what looks like the output of a computer translation step)

+def urlunsplit(_scheme_netloc_url_query_fragment_):
+    (scheme, netloc, url, query, fragment) =
_scheme_netloc_url_query_fragment_




----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2007-03-22 19:59

Message:
Logged In: YES 
user_id=357491
Originator: NO

The reason I mention using 2to3 is Collin has done a bunch of work on it
in order to make it also work with lambda.  I believe he also cleaned up
the generated names.

And as for cleaning up the patches, Tony, I can easily edit the diff file
manually, but thanks for the offer.  But if I need you to I will let you
know.

----------------------------------------------------------------------

Comment By: Tony Lownds (tonylownds)
Date: 2007-03-22 17:53

Message:
Logged In: YES 
user_id=24100
Originator: YES

Thanks for the comments.

Re: "stuff", if you look at other parts of the code, that is how the
triple is referred to -- fyi on how the name "stuff" came to be :) I was
also at a loss for a better name, and went with the contextual references.

Re: using 2to3 -- Brett, in many cases I actually tried to come up with a
consistent name. IIRC 2to3 will put in XXX_fixme_123 or something like
that. Or it did when I last tried. Anyway, if you end up using 2to3, I
would really appreciate it if you also picked up the inspect.py docstring
change.

Re: signature changes (eg LeftShift) and repr (FloorDiv) -- that is
generated code, and that is why "repr" is used. I chose to change the
generation source rather than the generation machinery as I was changing
the source anyway, to account for the changes to parameter syntax. That is
why there are API changes. Given that other API changes were made and those
class constructors are more or less an internal API (created through
compiler.parse),
I believe this is the best way of dealing with the changes.

Brett, If you need me to remove the 2to3 doc toolchain patches, I can do
that.


----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2007-03-22 15:57

Message:
Logged In: YES 
user_id=357491
Originator: NO

Chances are I will take Tony's patch for its removal of the feature and
use 2to3 to handle the conversion as much as possible.

----------------------------------------------------------------------

Comment By: Jim Jewett (jimjjewett)
Date: 2007-03-22 08:57

Message:
Logged In: YES 
user_id=764593
Originator: NO

On the reprs, is there a reason to call repr instead of using %r?

For example, the Floordiv repr changed to:

"FloorDiv(%s, %s)" % (repr(self.left), repr(self.right))

Why not "FloorDiv(%r, %r)" % (self.left, self.right)

or even, (assuming PEP 3101 with locals,)

"FloorDiv({self.left:r}, {self.right:r})".format()

----------------------------------------------------------------------

Comment By: Jim Jewett (jimjjewett)
Date: 2007-03-22 08:47

Message:
Logged In: YES 
user_id=764593
Originator: NO

Not sure if any of these are really problems, and it looks like Tony
already looked them over before submitting, but still worth mentioning...

(1)  This does more than take out tuples, though the other changes do also
look like things that ought to change for py3k.

(2)  I was amused by the name "stuff", but I couldn't think of a better
name for that particular grouping.  (suffix, mode, type)

(3)  There are some signature changes, where the unpacking is now done by
the caller, such as 

 class LeftShift(Node):
-    def __init__(self, (left, right), lineno=None):
+    def __init__(self, left, right, lineno=None):

(4)  The pdb change marks could be shortened a bit.  That shouldn't matter
for patching, but I'll post it here since it helped me.

-    def user_exception(self, frame, (exc_type, exc_value,
exc_traceback)):
+    def user_exception(self, frame, exc_info):
         """This function is called if an exception occurs,
         but only if we are to stop at or just below this level."""
+        (exc_type, exc_value, exc_traceback) = exc_info
         frame.f_locals['__exception__'] = exc_type, exc_value      
-        if type(exc_type) == type(''):
-            exc_type_name = exc_type
-        else: exc_type_name = exc_type.__name__
+        exc_type_name = exc_type.__name__
         print(exc_type_name + ':', _saferepr(exc_value),
file=self.stdout)
         self.interaction(frame, exc_traceback)


----------------------------------------------------------------------

Comment By: Tony Lownds (tonylownds)
Date: 2007-03-12 11:07

Message:
Logged In: YES 
user_id=24100
Originator: YES

I found some more code to be removed + a docstring fix in inspect.py.
getfullargspec's docstring lists the 
wrong order for return values. This patch combined the refmanual patch,
previous simplified args patch, 
and 2to3 changes on some doc building tools.

File Added: simplified-args.patch

----------------------------------------------------------------------

Comment By: Tony Lownds (tonylownds)
Date: 2007-03-11 18:34

Message:
Logged In: YES 
user_id=24100
Originator: YES

File Added: refmanual.patch

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1678060&group_id=5470


More information about the Patches mailing list