Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect inspect.Signature can be created: pos-only with a default followed by pos-or-kw without one #103556

Closed
sobolevn opened this issue Apr 15, 2023 · 0 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Apr 15, 2023

While working on #103553 I found one more problem that merits its own issue.

Right now it is impossible to create a function that have a pos-or-keyword parameter without a default comming after a pos-only parameter with a default. Demo:

>>> def test(pod=42, /, pk): ...
  File "<stdin>", line 1
    def test(pod=42, /, pk): ...
                        ^^
SyntaxError: parameter without a default follows parameter with a default

>>> lambda pod=42, /, pk: ...
  File "<stdin>", line 1
    lambda pod=42, /, pk: ...
                      ^^
SyntaxError: parameter without a default follows parameter with a default

But, it is possible to create a signature like this. Repro:

>>> import inspect
>>> def one(pk): ...
... 
>>> def two(pod=42, /): ...
... 
>>> sig1 = inspect.signature(one)
>>> sig2 = inspect.signature(two)
>>> inspect.Signature((sig2.parameters['pod'], sig1.parameters['pk']))
<Signature (pod=42, /, pk)>

This looks like a bug to me, there's no reason for a signature to behave differently and to support function signatures that cannot be created.

Desired behaviour:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython/ex.py", line 7, in <module>
    print(inspect.Signature((sig2.parameters['pod'], sig1.parameters['pk'])))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/inspect.py", line 3039, in __init__
    raise ValueError(msg)
ValueError: non-default argument follows default argument

The fix is quite easy. PR is incomming.

I've also tried the same thing with #103404 (because it is very related) but, it still does not solve this corner case.

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Apr 15, 2023
@sobolevn sobolevn self-assigned this Apr 15, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Apr 15, 2023
carljm pushed a commit that referenced this issue Apr 22, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 22, 2023
…t default after pos-only with default (pythonGH-103557)

(cherry picked from commit 6b58d41)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
carljm pushed a commit that referenced this issue Apr 22, 2023
@carljm carljm closed this as completed Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants