diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 89af8e465464..a0f886106715 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -654,10 +654,10 @@ def _verify_arg_name( if is_dunder(function_name, exclude_special=True): return - def strip_prefix(s: str, prefix: str) -> str: - return s.removeprefix(prefix) - - if strip_prefix(stub_arg.variable.name, "__") == runtime_arg.name: + if ( + stub_arg.variable.name == runtime_arg.name + or stub_arg.variable.name.removeprefix("__") == runtime_arg.name + ): return nonspecific_names = {"object", "args"} @@ -948,6 +948,7 @@ def _verify_signature( if ( runtime_arg.kind != inspect.Parameter.POSITIONAL_ONLY and (stub_arg.pos_only or stub_arg.variable.name.startswith("__")) + and not runtime_arg.name.startswith("__") and stub_arg.variable.name.strip("_") != "self" and not is_dunder(function_name, exclude_special=True) # noisy for dunder methods ): diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 099e7605eea2..492897d33a4a 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -339,6 +339,21 @@ def __exit__(self, exc_type, exc_val, exc_tb): pass """, error=None, ) + yield Case( + stub="""def dunder_name(__x: int) -> None: ...""", + runtime="""def dunder_name(__x: int) -> None: ...""", + error=None, + ) + yield Case( + stub="""def dunder_name_posonly(__x: int, /) -> None: ...""", + runtime="""def dunder_name_posonly(__x: int) -> None: ...""", + error=None, + ) + yield Case( + stub="""def dunder_name_bad(x: int) -> None: ...""", + runtime="""def dunder_name_bad(__x: int) -> None: ...""", + error="dunder_name_bad", + ) @collect_cases def test_arg_kind(self) -> Iterator[Case]: