Skip to content

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

Closed

Description

@ApostolFet

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 4, 2024
  2. Eclips4 commented on Dec 4, 2024

    @Eclips4
    Member

    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
    >>>
  3. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Dec 4, 2024
  4. skirpichev commented on Dec 5, 2024

    @skirpichev
    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?!

  5. serhiy-storchaka commented on Dec 5, 2024

    @serhiy-storchaka
    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.

  6. added a commit that references this issue on Dec 8, 2024
  7. serhiy-storchaka commented on Dec 8, 2024

    @serhiy-storchaka
    Member

    Thank you for your contribution, @ApostolFet.

  8. added a commit that references this issue on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions