From 754596cb7fd36230376a9aa96cfa07b79a627467 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 5 Mar 2025 13:05:38 -0800 Subject: [PATCH 1/2] stubtest: better checking of runtime args with dunder names Fixes #15302, fixes #14560 --- mypy/stubtest.py | 6 ++---- mypy/test/teststubtest.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 89af8e465464..a444cdf83af4 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -654,10 +654,7 @@ 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 +945,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..07dda2696db0 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -339,6 +339,22 @@ 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]: From 969cc0f88611a1db3bad36423b0e1c9eaeb8764a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Mar 2025 21:07:44 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/stubtest.py | 5 ++++- mypy/test/teststubtest.py | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index a444cdf83af4..a0f886106715 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -654,7 +654,10 @@ def _verify_arg_name( if is_dunder(function_name, exclude_special=True): return - if stub_arg.variable.name == runtime_arg.name or stub_arg.variable.name.removeprefix("__") == 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"} diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 07dda2696db0..492897d33a4a 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -355,7 +355,6 @@ def __exit__(self, exc_type, exc_val, exc_tb): pass error="dunder_name_bad", ) - @collect_cases def test_arg_kind(self) -> Iterator[Case]: yield Case(