Skip to content

Commit 2110551

Browse files
authored
bpo-39775: inspect: Change Signature.parameters back to OrderedDict. (GH-18684)
1 parent 9f1cb1b commit 2110551

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

Doc/library/inspect.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,18 +624,15 @@ function.
624624

625625
.. attribute:: Signature.parameters
626626

627-
An dictionary of :class:`Parameter` objects. Parameters appear in strict
628-
definition order, including keyword-only parameters.
627+
An ordered mapping of parameters' names to the corresponding
628+
:class:`Parameter` objects. Parameters appear in strict definition
629+
order, including keyword-only parameters.
629630

630631
.. versionchanged:: 3.7
631632
Python only explicitly guaranteed that it preserved the declaration
632633
order of keyword-only parameters as of version 3.7, although in practice
633634
this order had always been preserved in Python 3.
634635

635-
.. versionchanged:: 3.9
636-
:attr:`parameters` is now of type :class:`dict`. Formerly, it was of
637-
type :class:`collections.OrderedDict`.
638-
639636
.. attribute:: Signature.return_annotation
640637

641638
The "return" annotation for the callable. If the callable has no "return"
@@ -824,7 +821,7 @@ function.
824821

825822
.. attribute:: BoundArguments.arguments
826823

827-
An ordered, mutable mapping of parameters' names to arguments' values.
824+
A mutable mapping of parameters' names to arguments' values.
828825
Contains only explicitly bound arguments. Changes in :attr:`arguments`
829826
will reflect in :attr:`args` and :attr:`kwargs`.
830827

Doc/whatsnew/3.9.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ now raises :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
218218
import attempts.
219219
(Contributed by Ngalim Siregar in :issue:`37444`.)
220220

221+
inspect
222+
-------
223+
224+
:attr:`inspect.BoundArguments.arguments` is changed from ``OrderedDict`` to regular
225+
dict. (Contributed by Inada Naoki in :issue:`36350` and :issue:`39775`.)
226+
221227
ipaddress
222228
---------
223229

Lib/inspect.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import functools
4949
import builtins
5050
from operator import attrgetter
51-
from collections import namedtuple
51+
from collections import namedtuple, OrderedDict
5252

5353
# Create constants for the compiler flags in Include/code.h
5454
# We try to get them from dis to avoid duplication
@@ -1727,7 +1727,7 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
17271727
"""
17281728

17291729
old_params = wrapped_sig.parameters
1730-
new_params = {}
1730+
new_params = OrderedDict(old_params.items())
17311731

17321732
partial_args = partial.args or ()
17331733
partial_keywords = partial.keywords or {}
@@ -1743,7 +1743,6 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
17431743

17441744

17451745
transform_to_kwonly = False
1746-
kwonly_params = {} # Keyword only parameters are moved to end.
17471746
for param_name, param in old_params.items():
17481747
try:
17491748
arg_value = ba.arguments[param_name]
@@ -1753,6 +1752,7 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
17531752
if param.kind is _POSITIONAL_ONLY:
17541753
# If positional-only parameter is bound by partial,
17551754
# it effectively disappears from the signature
1755+
new_params.pop(param_name)
17561756
continue
17571757

17581758
if param.kind is _POSITIONAL_OR_KEYWORD:
@@ -1771,26 +1771,28 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
17711771
# multiple values.
17721772
transform_to_kwonly = True
17731773
# Set the new default value
1774-
param = param.replace(default=arg_value)
1774+
new_params[param_name] = param.replace(default=arg_value)
17751775
else:
17761776
# was passed as a positional argument
1777+
new_params.pop(param.name)
17771778
continue
17781779

17791780
if param.kind is _KEYWORD_ONLY:
17801781
# Set the new default value
1781-
param = param.replace(default=arg_value)
1782+
new_params[param_name] = param.replace(default=arg_value)
17821783

17831784
if transform_to_kwonly:
17841785
assert param.kind is not _POSITIONAL_ONLY
17851786

17861787
if param.kind is _POSITIONAL_OR_KEYWORD:
1787-
kwonly_params[param_name] = param.replace(kind=_KEYWORD_ONLY)
1788+
new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1789+
new_params[param_name] = new_param
1790+
new_params.move_to_end(param_name)
17881791
elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1789-
kwonly_params[param_name] = param
1790-
else:
1791-
new_params[param_name] = param
1792+
new_params.move_to_end(param_name)
1793+
elif param.kind is _VAR_POSITIONAL:
1794+
new_params.pop(param.name)
17921795

1793-
new_params.update(kwonly_params)
17941796
return wrapped_sig.replace(parameters=new_params.values())
17951797

17961798

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change ``inspect.Signature.parameters`` back to ``collections.OrderedDict``.
2+
This was changed to ``dict`` in Python 3.9.0a4.

0 commit comments

Comments
 (0)