Skip to content
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

stubtest: fix pos-only handling in overload resolution #16750

Merged
merged 2 commits into from
Jan 12, 2024
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
7 changes: 6 additions & 1 deletion mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ def from_overloadedfuncdef(stub: nodes.OverloadedFuncDef) -> Signature[nodes.Arg
# argument. To accomplish this, we just make up a fake index-based name.
name = (
f"__{index}"
if arg.variable.name.startswith("__") or assume_positional_only
if arg.variable.name.startswith("__")
or arg.pos_only
or assume_positional_only
or arg.variable.name.strip("_") == "self"
else arg.variable.name
)
all_args.setdefault(name, []).append((arg, index))
Expand Down Expand Up @@ -872,6 +875,7 @@ def get_kind(arg_name: str) -> nodes.ArgKind:
type_annotation=None,
initializer=None,
kind=get_kind(arg_name),
pos_only=all(arg.pos_only for arg, _ in all_args[arg_name]),
)
if arg.kind.is_positional():
sig.pos.append(arg)
Expand Down Expand Up @@ -907,6 +911,7 @@ def _verify_signature(
if (
runtime_arg.kind != inspect.Parameter.POSITIONAL_ONLY
and (stub_arg.pos_only or stub_arg.variable.name.startswith("__"))
and stub_arg.variable.name.strip("_") != "self"
and not is_dunder(function_name, exclude_special=True) # noisy for dunder methods
):
yield (
Expand Down
58 changes: 57 additions & 1 deletion mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ def test(*args: Any, **kwargs: Any) -> None:
)

actual_errors = set(output.splitlines())
assert actual_errors == expected_errors, output
if actual_errors != expected_errors:
output = run_stubtest(
stub="\n\n".join(textwrap.dedent(c.stub.lstrip("\n")) for c in cases),
runtime="\n\n".join(textwrap.dedent(c.runtime.lstrip("\n")) for c in cases),
options=[],
)
assert actual_errors == expected_errors, output

return test

Expand Down Expand Up @@ -660,6 +666,56 @@ def f6(self, x, /): pass
""",
error=None,
)
yield Case(
stub="""
@overload
def f7(a: int, /) -> int: ...
@overload
def f7(b: str, /) -> str: ...
""",
runtime="def f7(x, /): pass",
error=None,
)
yield Case(
stub="""
@overload
def f8(a: int, c: int = 0, /) -> int: ...
@overload
def f8(b: str, d: int, /) -> str: ...
""",
runtime="def f8(x, y, /): pass",
error="f8",
)
yield Case(
stub="""
@overload
def f9(a: int, c: int = 0, /) -> int: ...
@overload
def f9(b: str, d: int, /) -> str: ...
""",
runtime="def f9(x, y=0, /): pass",
error=None,
)
yield Case(
stub="""
class Bar:
@overload
def f1(self) -> int: ...
Copy link
Collaborator Author

@hauntsaninja hauntsaninja Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have instances of this in python/typeshed#11250, I don't think it's worth having stubtest complain that this isn't positional-only, at least for now

@overload
def f1(self, a: int, /) -> int: ...

@overload
def f2(self, a: int, /) -> int: ...
@overload
def f2(self, a: str, /) -> int: ...
""",
runtime="""
class Bar:
def f1(self, *a) -> int: ...
def f2(self, *a) -> int: ...
""",
error=None,
)

@collect_cases
def test_property(self) -> Iterator[Case]:
Expand Down