]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39775: inspect: Change Signature.parameters back to OrderedDict. (GH-18684)
authorInada Naoki <songofacandy@gmail.com>
Mon, 2 Mar 2020 09:54:49 +0000 (18:54 +0900)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2020 09:54:48 +0000 (18:54 +0900)
Doc/library/inspect.rst
Doc/whatsnew/3.9.rst
Lib/inspect.py
Misc/NEWS.d/next/Library/2020-02-28-16-42-16.bpo-39775.IuSvVb.rst [new file with mode: 0644]

index 9b9bc99f43d48c644cd9ec62f4d7635314cb1b4e..d00a30ff0040631656a161a349ec56b066808ab6 100644 (file)
@@ -624,18 +624,15 @@ function.
 
    .. attribute:: Signature.parameters
 
-      An dictionary of :class:`Parameter` objects.  Parameters appear in strict
-      definition order, including keyword-only parameters.
+      An ordered mapping of parameters' names to the corresponding
+      :class:`Parameter` objects.  Parameters appear in strict definition
+      order, including keyword-only parameters.
 
       .. versionchanged:: 3.7
          Python only explicitly guaranteed that it preserved the declaration
          order of keyword-only parameters as of version 3.7, although in practice
          this order had always been preserved in Python 3.
 
-      .. versionchanged:: 3.9
-         :attr:`parameters` is now of type :class:`dict`. Formerly, it was of
-         type :class:`collections.OrderedDict`.
-
    .. attribute:: Signature.return_annotation
 
       The "return" annotation for the callable.  If the callable has no "return"
@@ -824,7 +821,7 @@ function.
 
    .. attribute:: BoundArguments.arguments
 
-      An ordered, mutable mapping of parameters' names to arguments' values.
+      A mutable mapping of parameters' names to arguments' values.
       Contains only explicitly bound arguments.  Changes in :attr:`arguments`
       will reflect in :attr:`args` and :attr:`kwargs`.
 
index 3364f392f15c77e400b7453ceca5c73b59d44cd9..f49575d89da678ec287c9a529590b4f2e5011e2c 100644 (file)
@@ -218,6 +218,12 @@ now raises :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
 import attempts.
 (Contributed by Ngalim Siregar in :issue:`37444`.)
 
+inspect
+-------
+
+:attr:`inspect.BoundArguments.arguments` is changed from ``OrderedDict`` to regular
+dict.  (Contributed by Inada Naoki in :issue:`36350` and :issue:`39775`.)
+
 ipaddress
 ---------
 
index 950bdb221798d6427061a2c7ea48a7dcc6a70861..bb82f96fdf374e94af94d66ff37bf2808d3bcecd 100644 (file)
@@ -48,7 +48,7 @@ import warnings
 import functools
 import builtins
 from operator import attrgetter
-from collections import namedtuple
+from collections import namedtuple, OrderedDict
 
 # Create constants for the compiler flags in Include/code.h
 # We try to get them from dis to avoid duplication
@@ -1727,7 +1727,7 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
     """
 
     old_params = wrapped_sig.parameters
-    new_params = {}
+    new_params = OrderedDict(old_params.items())
 
     partial_args = partial.args or ()
     partial_keywords = partial.keywords or {}
@@ -1743,7 +1743,6 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
 
 
     transform_to_kwonly = False
-    kwonly_params = {}  # Keyword only parameters are moved to end.
     for param_name, param in old_params.items():
         try:
             arg_value = ba.arguments[param_name]
@@ -1753,6 +1752,7 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
             if param.kind is _POSITIONAL_ONLY:
                 # If positional-only parameter is bound by partial,
                 # it effectively disappears from the signature
+                new_params.pop(param_name)
                 continue
 
             if param.kind is _POSITIONAL_OR_KEYWORD:
@@ -1771,26 +1771,28 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
                     # multiple values.
                     transform_to_kwonly = True
                     # Set the new default value
-                    param = param.replace(default=arg_value)
+                    new_params[param_name] = param.replace(default=arg_value)
                 else:
                     # was passed as a positional argument
+                    new_params.pop(param.name)
                     continue
 
             if param.kind is _KEYWORD_ONLY:
                 # Set the new default value
-                param = param.replace(default=arg_value)
+                new_params[param_name] = param.replace(default=arg_value)
 
         if transform_to_kwonly:
             assert param.kind is not _POSITIONAL_ONLY
 
             if param.kind is _POSITIONAL_OR_KEYWORD:
-                kwonly_params[param_name] = param.replace(kind=_KEYWORD_ONLY)
+                new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
+                new_params[param_name] = new_param
+                new_params.move_to_end(param_name)
             elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
-                kwonly_params[param_name] = param
-        else:
-            new_params[param_name] = param
+                new_params.move_to_end(param_name)
+            elif param.kind is _VAR_POSITIONAL:
+                new_params.pop(param.name)
 
-    new_params.update(kwonly_params)
     return wrapped_sig.replace(parameters=new_params.values())
 
 
diff --git a/Misc/NEWS.d/next/Library/2020-02-28-16-42-16.bpo-39775.IuSvVb.rst b/Misc/NEWS.d/next/Library/2020-02-28-16-42-16.bpo-39775.IuSvVb.rst
new file mode 100644 (file)
index 0000000..1667b43
--- /dev/null
@@ -0,0 +1,2 @@
+Change ``inspect.Signature.parameters`` back to ``collections.OrderedDict``.
+This was changed to ``dict`` in Python 3.9.0a4.