Skip to content

Changing the number of function arguments when using a decorator does not work. #9505

Open

Description

Bug description

from typing import Callable, Concatenate, ParamSpec, TypeVar

S = TypeVar('S')
P = ParamSpec('P')
RealFun = Callable[Concatenate[S, P], None]
FunWithEvent = Callable[Concatenate[S, int, P], None]


def run_event() -> Callable[[RealFun[S, P]], FunWithEvent[S, P]]:
    def wrapper(func: RealFun[S, P]) -> FunWithEvent[S, P]:
        def wrapped(self: S, event: int, *args: P.args, **kwargs: P.kwargs) -> None:
            func(self, *args, **kwargs)
        return wrapped
    return wrapper


class SomeClass:
    @run_event()
    def orig_fun(self) -> None:
        pass

    def do_call1(self) -> None:
        self.orig_fun(42) # That's OK

    def do_call2(self) -> None:
        self.orig_fun()  # This is an error, MyPy finds that, Nice

    def do_call3(self) -> None:
        self.orig_fun('q')  # This is an error, MyPy finds that, Nice

    def do_call4(self) -> None:
        self.orig_fun(42, 'q')  # This is an error, MyPy finds that, Nice

Configuration

No response

Command used

pylint --rcfile rcfile --disable=similarities --jobs jobs paths

Pylint output

E1121: Too many positional arguments for method call (too-many-function-args)

Expected behavior

If you use a decorator, you can add an argument of a certain type to the function. But if the argument type does not match the one specified in the decorator, an error will occur. It will also be an error if you don't specify an argument at all.

Pylint version

pylint 3.0.4
astroid 3.0.3
Python 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]

OS / Environment

No response

Additional dependencies

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲DecoratorsNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions