pylint icon indicating copy to clipboard operation
pylint copied to clipboard

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

Open SVETIK-LION opened this issue 1 year ago • 2 comments

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

SVETIK-LION avatar Mar 15 '24 17:03 SVETIK-LION

Link with code to mypy Playground https://mypy-play.net/?mypy=latest&python=3.12&gist=c0cce297d05269e23040f01a6cc80c0e

SVETIK-LION avatar Mar 16 '24 15:03 SVETIK-LION

Thank you for opening the issue. pylint does not understand decorators well, generally.

Pierre-Sassoulas avatar Mar 18 '24 12:03 Pierre-Sassoulas