Skip to content

Commit 754596c

Browse files
committed
stubtest: better checking of runtime args with dunder names
Fixes #15302, fixes #14560
1 parent b1ac028 commit 754596c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

mypy/stubtest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,7 @@ def _verify_arg_name(
654654
if is_dunder(function_name, exclude_special=True):
655655
return
656656

657-
def strip_prefix(s: str, prefix: str) -> str:
658-
return s.removeprefix(prefix)
659-
660-
if strip_prefix(stub_arg.variable.name, "__") == runtime_arg.name:
657+
if stub_arg.variable.name == runtime_arg.name or stub_arg.variable.name.removeprefix("__") == runtime_arg.name:
661658
return
662659

663660
nonspecific_names = {"object", "args"}
@@ -948,6 +945,7 @@ def _verify_signature(
948945
if (
949946
runtime_arg.kind != inspect.Parameter.POSITIONAL_ONLY
950947
and (stub_arg.pos_only or stub_arg.variable.name.startswith("__"))
948+
and not runtime_arg.name.startswith("__")
951949
and stub_arg.variable.name.strip("_") != "self"
952950
and not is_dunder(function_name, exclude_special=True) # noisy for dunder methods
953951
):

mypy/test/teststubtest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,22 @@ def __exit__(self, exc_type, exc_val, exc_tb): pass
339339
""",
340340
error=None,
341341
)
342+
yield Case(
343+
stub="""def dunder_name(__x: int) -> None: ...""",
344+
runtime="""def dunder_name(__x: int) -> None: ...""",
345+
error=None,
346+
)
347+
yield Case(
348+
stub="""def dunder_name_posonly(__x: int, /) -> None: ...""",
349+
runtime="""def dunder_name_posonly(__x: int) -> None: ...""",
350+
error=None,
351+
)
352+
yield Case(
353+
stub="""def dunder_name_bad(x: int) -> None: ...""",
354+
runtime="""def dunder_name_bad(__x: int) -> None: ...""",
355+
error="dunder_name_bad",
356+
)
357+
342358

343359
@collect_cases
344360
def test_arg_kind(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)