Skip to content

Commit a067d84

Browse files
authored
stubtest: better checking of runtime args with dunder names (#18756)
Fixes #15302, fixes #14560. Linking #18343
1 parent efc045e commit a067d84

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

mypy/stubtest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,10 @@ 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 (
658+
stub_arg.variable.name == runtime_arg.name
659+
or stub_arg.variable.name.removeprefix("__") == runtime_arg.name
660+
):
661661
return
662662

663663
nonspecific_names = {"object", "args"}
@@ -948,6 +948,7 @@ def _verify_signature(
948948
if (
949949
runtime_arg.kind != inspect.Parameter.POSITIONAL_ONLY
950950
and (stub_arg.pos_only or stub_arg.variable.name.startswith("__"))
951+
and not runtime_arg.name.startswith("__")
951952
and stub_arg.variable.name.strip("_") != "self"
952953
and not is_dunder(function_name, exclude_special=True) # noisy for dunder methods
953954
):

mypy/test/teststubtest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,21 @@ 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+
)
342357

343358
@collect_cases
344359
def test_arg_kind(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)