Skip to content

stubtest: better checking of runtime args with dunder names #18756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
):
Expand Down
15 changes: 15 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down