Skip to content

Commit

Permalink
stubtest: fix pos-only handling in overload resolution (#16750)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Jan 12, 2024
1 parent edebe02 commit 1fd29ac
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
7 changes: 6 additions & 1 deletion mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,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 @@ -870,6 +873,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 @@ -905,6 +909,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: ...
@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

0 comments on commit 1fd29ac

Please sign in to comment.