Skip to content

self not considered positional-only when followed by **kwargs #19051

@osandov

Description

@osandov

Bug Report

The following example using Concatenate to represent a method passes mypy in strict mode:

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=5d5a1b27f2838371bee58b2d9c9a7721

from collections.abc import Callable
from typing import Any, Concatenate


def func(x: int) -> None:
    return


class MyClass:
    def method(self, x: int) -> None:
        return


def check[**P](f: Callable[P, None], m: Callable[Concatenate[Any, P], None]) -> None:
    return


check(func, MyClass.method)

However, if the signature has **kwargs, it fails with Argument 2 to "check" has incompatible type ... This is likely because "method of MyClass" has named arguments: "self".

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=ef01c9fd1251d95be27ce96401ec7fe8

from collections.abc import Callable
from typing import Any, Concatenate


def func(**kwargs: Any) -> None:
    return


class MyClass:
    def method(self, **kwargs: Any) -> None:
        return


def check[**P](f: Callable[P, None], m: Callable[Concatenate[Any, P], None]) -> None:
    return


check(func, MyClass.method)

Explicitly making self positional-only fixes this, but in my case, I don't control the method signature (in my actual use case, the method is argparse.ArgumentParser.add_argument() and the function is copy_method_params() from python/cpython#121693).

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=ef01c9fd1251d95be27ce96401ec7fe8

Expected Behavior

There should be no errors.

Actual Behavior

There is an error:

main.py:18: error: Argument 2 to "check" has incompatible type "Callable[[Arg(MyClass, 'self'), KwArg(Any)], None]"; expected "Callable[[Any, KwArg(Any)], None]"  [arg-type]
main.py:18: note: This is likely because "method of MyClass" has named arguments: "self". Consider marking them positional-only
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: master, latest version
  • Mypy command-line flags: --strict

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions