[Python-Dev] Can Python guarantee the order of keyword-only parameters?
Yury Selivanov
yselivanov.ml at gmail.com
Mon Nov 27 18:58:01 EST 2017
On Mon, Nov 27, 2017 at 12:05 PM, Larry Hastings <larry at hastings.org> wrote:
[..]
> The PEP for
> inspect.signature (PEP 362) says that when comparing two signatures for
> equality, their positional and positional-or-keyword parameters must be in
> the same order. It makes a point of *not* requiring that the two functions'
> keyword-only parameters be in the same order.
Yes, and I believe Signature.__eq__ should stay that way.
>
> For every currently supported version of Python 3, inspect.signature and
> fn.__code__.co_varnames preserve the order of keyword-only parameters. This
> isn't surprising; it's basically the same code path implementing those as
> the two types of positional-relevant parameters, so the most straightforward
> implementation would naturally preserve their order. It's just not
> guaranteed.
>
> I'd like inspect.signature to guarantee that the order of keyword-only
> parameters always matches the order they were declared in. Technically this
> isn't a language feature, it's a library feature. But making this guarantee
> would require that CPython internally cooperate, so it's kind of a language
> feature too.
We can update the documentation and say that we preserve the order in
simple cases:
def foo(*, a=1, b=2): pass
s = inspect.signature(foo)
assert list(s.parameters.keys()) == ['a', 'b']
We can't say anything about the order if someone passes a partial
object, or sets custom Signature objects to func.__signature__.
Yury
More information about the Python-Dev
mailing list