classification
Title: BoundArguments.arguments should be unordered
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, cheryl.sabella, yselivanov
Priority: normal Keywords: patch

Created on 2014-12-18 12:56 by Antony.Lee, last changed 2020-03-02 11:10 by Antony.Lee. This issue is now closed.

Files
File name Uploaded Description Edit
unordered-boundarguments.patch Antony.Lee, 2014-12-18 12:56 review
Messages (4)
msg232874 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-12-18 12:56
This patch makes BoundArguments.arguments an unordered dict.  As discussed on python-ideas, the rationale for this is

1. The current ordering in ba.arguments is the one of the parameters in the signature (which is already available via the ba.signature attribute), not the one in which the arguments are given (which will always be unavailable as long as Python doesn't keep the order of keyword arguments), so no information is lost by this patch.

2. The recipe in the docs for updating ba.arguments to also contain default argument values breaks that ordering, making __eq__ behavior difficult to explain:

s = signature(lambda x=None, y=None: None)
ba0 = s.bind()
ba1 = s.bind(x=None)
ba2 = s.bind(y=None)
<add default values as suggested in the docs>

At that point, with the current implementation, we'll have ba0 == ba1 != ba2... why?

3. Implementing ba.arguments as a regular dict makes signature.bind much faster, which is important e.g. if it is used in a decorating type-checker as suggested in PEP362.  (That specific issue could also be improved by a C-implemented OrderedDict, but the other reasons remain valid.)
msg232923 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-12-19 02:54
I'd like to see PEP 468 explicitly rejected or postponed till 3.6 before we make this change in 3.5.
msg310769 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-01-26 15:43
I know there are other issues to remove OrderedDicts, so should this patch be used for BoundArguments?
msg362799 - (view) Author: Antony Lee (Antony.Lee) * Date: 2020-02-27 13:41
Done in bugs.python.org/issue36350 / https://github.com/python/cpython/pull/12412.
History
Date User Action Args
2020-03-02 11:10:54Antony.Leesetnosy: - Antony.Lee
2020-02-27 13:41:48Antony.Leesetstatus: open -> closed
resolution: fixed
messages: + msg362799

stage: needs patch -> resolved
2018-01-26 15:44:00cheryl.sabellasetstage: needs patch
type: enhancement
versions: + Python 3.7, - Python 3.5
2018-01-26 15:43:35cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg310769
2014-12-20 02:02:21rhettingersetnosy: + brett.cannon
2014-12-19 02:54:26yselivanovsetmessages: + msg232923
2014-12-19 02:05:55berker.peksagsetnosy: + yselivanov
2014-12-18 12:56:29Antony.Leecreate