You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a strange case. It looks like “iterable” is half-supported as a keyword argument. So Silent Ghost’s patch fixes the signature, but the code still tries to accept keyword arguments:
>>> sorted(iterable=None)
TypeError: 'NoneType' object is not iterable
>>> sorted(iterable=())
TypeError: 'iterable' is an invalid keyword argument for this function
The problem is that sorted() blindly passes the keyword arguments to list.sort(). I guess we could delete "iterable" from them, but maybe it is not worth the trouble.
eriknw commented on Apr 12, 2016
eriknwmannequin
MannequinAuthor
More actions
Interesting observation, Martin.
Upon further consideration, the call signature for sorted really is quite odd. It doesn't behave like any other builtin function. Currently, "iterable" is positional-only, and "key=" and "reverse=" are keyword only. I would only expect such behavior for functions with variadic *args.
I uploaded a new patch so that the call signature matches the original __text_signature__. This means "iterable" may be given as a keyword argument, and "key" and "reverse" may be given as positional arguments.
I added tests for the new behavior, and all tests pass for me.
I don't think we should start down the path of changing APIs just to accommodate weakness in the generation of the text signature.
We don't want to encourage unreadable oddities like sorted(reverse=False, iterable=source). Best readability comes from the required positional argument for the iterable. Please don't create a new and unnecessary keyword argument. The current API is intentional.
That's a fair and valid point, Raymond. "sorted_2.patch" was submitted for consideration. Either __text_signature__ is wrong, or the call argument handling is wrong. One should be fixed.
Having a flexible call signature as if sorted were a user-defined function, such as "def sorted(iterable, key=None, reverse=False):", does allow for programmatic use of the introspected signature. Here, using "iterable=" as a keyword can be convenient.
"sorted_1.diff" is wrong. To match the existing call signature, __text_signature__ should be:
I don't know any other builtin with a signature like this. Such a signature may be a point of confusion for people learning Python ("what are those funny symbols?!"), who often encounter sorted early on.
Activity
eriknw commentedon Apr 10, 2016
The first argument to sorted is positional-only, so the text signature should be:
instead of
To reproduce the issue, attempt to use "iterable" as a keyword argument:
vadmium commentedon Apr 10, 2016
This is a strange case. It looks like “iterable” is half-supported as a keyword argument. So Silent Ghost’s patch fixes the signature, but the code still tries to accept keyword arguments:
The problem is that sorted() blindly passes the keyword arguments to list.sort(). I guess we could delete "iterable" from them, but maybe it is not worth the trouble.
eriknw commentedon Apr 12, 2016
Interesting observation, Martin.
Upon further consideration, the call signature for sorted really is quite odd. It doesn't behave like any other builtin function. Currently, "iterable" is positional-only, and "key=" and "reverse=" are keyword only. I would only expect such behavior for functions with variadic *args.
I uploaded a new patch so that the call signature matches the original __text_signature__. This means "iterable" may be given as a keyword argument, and "key" and "reverse" may be given as positional arguments.
I added tests for the new behavior, and all tests pass for me.
rhettinger commentedon Apr 12, 2016
I don't think we should start down the path of changing APIs just to accommodate weakness in the generation of the text signature.
We don't want to encourage unreadable oddities like sorted(reverse=False, iterable=source). Best readability comes from the required positional argument for the iterable. Please don't create a new and unnecessary keyword argument. The current API is intentional.
eriknw commentedon Apr 12, 2016
That's a fair and valid point, Raymond. "sorted_2.patch" was submitted for consideration. Either __text_signature__ is wrong, or the call argument handling is wrong. One should be fixed.
Having a flexible call signature as if sorted were a user-defined function, such as "def sorted(iterable, key=None, reverse=False):", does allow for programmatic use of the introspected signature. Here, using "iterable=" as a keyword can be convenient.
"sorted_1.diff" is wrong. To match the existing call signature, __text_signature__ should be:
I don't know any other builtin with a signature like this. Such a signature may be a point of confusion for people learning Python ("what are those funny symbols?!"), who often encounter sorted early on.
serhiy-storchaka commentedon Apr 12, 2016
See bpo-26282.
ncoghlan commentedon Apr 12, 2016
+1 for Serhiy's suggestion of enhancing the C API to specifically handle these cases.
vadmium commentedon Apr 12, 2016
I didn’t realize about the keyword-only parameters. This is inherited from list.sort(). The signature can be corrected in 3.5.
There is also bpo-21314 about documenting the slash notation for signatures (it comes from PEP-457).
eriknw commentedon Apr 12, 2016
sorted_3.patch corrects the __text_signature__. Behavior of sorted is unchanged.
vadmium commentedon Apr 12, 2016
Patch 3 looks okay to me.
serhiy-storchaka commentedon Jan 20, 2017
See also bpo-29327 and bpo-29331.
sorted_3.patch LGTM.
rhettinger commentedon Jan 23, 2017
Serhiy, do you want to apply this?
python-dev commentedon Jan 23, 2017
New changeset c0a9fb3e19b9 by Serhiy Storchaka in branch '3.5':
Issue bpo-26729: Fixed __text_signature__ for sorted().
https://hg.python.org/cpython/rev/c0a9fb3e19b9
New changeset fcb19fb42058 by Serhiy Storchaka in branch '3.6':
Issue bpo-26729: Fixed __text_signature__ for sorted().
https://hg.python.org/cpython/rev/fcb19fb42058
New changeset 4ad195d9e184 by Serhiy Storchaka in branch 'default':
Issue bpo-26729: Fixed __text_signature__ for sorted().
https://hg.python.org/cpython/rev/4ad195d9e184