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

No validation on erroneous creation of 2 or more parameter variables (VAR_KEYWORD and VAR_POSITIONAL) in inspect.Signature #127610

Closed
ApostolFet opened this issue Dec 4, 2024 · 4 comments
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ApostolFet
Copy link
Contributor

ApostolFet commented Dec 4, 2024

Bug report

Bug description:

We can create inspect.Signature with two or more variable parameters, but this will be syntactically incorrect when defining the function manually:

import inspect

parameters = [
    inspect.Parameter("args", kind=inspect.Parameter.VAR_POSITIONAL),
    inspect.Parameter("other_args", kind=inspect.Parameter.VAR_POSITIONAL),
    inspect.Parameter("kwargs", kind=inspect.Parameter.VAR_KEYWORD),
    inspect.Parameter("other_kwargs", kind=inspect.Parameter.VAR_KEYWORD),
]

signature = inspect.Signature(parameters)
print(signature)

OUTPUT:

(*args, *other_args, **kwargs, **other_kwargs)

And naturally we get a syntax error when defining a function with such a signature:

def func(*args, *other_args, **kwargs, **other_kwargs):
    ...

OUTPUT:

  File "/home/apostol_fet/inspect_signature.py", line 13
    def func(*args, *other_args, **kwargs, **other_kwargs):
                    ^
SyntaxError: * argument may appear only once

Can we add validations for this case in inspect.Signature.__init__?
I would like to send a PR to fix this if it is not intended behavior.

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs

@ApostolFet ApostolFet added the type-bug An unexpected behavior, bug, or error label Dec 4, 2024
@Eclips4 Eclips4 added the stdlib Python modules in the Lib dir label Dec 4, 2024
@Eclips4
Copy link
Member

Eclips4 commented Dec 4, 2024

cc @serhiy-storchaka

I do think that this check would be useful. We already have a check for situations where user is trying to create a signature with a variadic keyword parameter before a variadic positional parameter:

>>> import inspect
>>> parameters = [
...     inspect.Parameter("args", kind=inspect.Parameter.VAR_KEYWORD),
...     inspect.Parameter("kwargs", kind=inspect.Parameter.VAR_POSITIONAL),
... ]
>>> inspect.Signature(parameters)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    inspect.Signature(parameters)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/home/eclips4/programming/programming-languages/cpython/Lib/inspect.py", line 2958, in __init__
    raise ValueError(msg)
ValueError: wrong parameter order: variadic keyword parameter before variadic positional parameter
>>>

@Eclips4 Eclips4 added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Dec 4, 2024
@skirpichev
Copy link
Member

I would like to send a PR to fix this if it is not intended behavior.

Definitely is a bug. Why do we want to produce syntactically incorrect Signature objects?!

@serhiy-storchaka
Copy link
Member

I agree. There are other checks for the order of parameters, for example positional or keyword parameter is not allowed before positional-only parameter. We should simply add more checks: for var-positional parameter before var-positional parameter and var-keyword parameter before any parameter.

@serhiy-storchaka
Copy link
Member

Thank you for your contribution, @ApostolFet.

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants