Skip to content

Commit

Permalink
Modified the overload call evaluation logic to conform with the new p…
Browse files Browse the repository at this point in the history
…roposed type specification rules regarding `*args` parameters. This addresses #9748.
  • Loading branch information
erictraut committed Jan 25, 2025
1 parent c9d4475 commit 9295635
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 1 addition & 5 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9432,7 +9432,7 @@ export function createTypeEvaluator(

// Start by evaluating the types of the arguments without any expected
// type. Also, filter the list of overloads based on the number of
// positional and named arguments that are present. We do all of this
// positional and keyword arguments that are present. We do all of this
// speculatively because we don't want to record any types in the type
// cache or record any diagnostics at this stage.
useSpeculativeMode(speculativeNode, () => {
Expand Down Expand Up @@ -10864,10 +10864,6 @@ export function createTypeEvaluator(
errorNode,
/* emitNotIterableError */ false
)?.type;

if (paramInfo.param.category !== ParamCategory.ArgsList) {
matchedUnpackedListOfUnknownLength = true;
}
}

const funcArg: Arg | undefined = listElementType
Expand Down
38 changes: 24 additions & 14 deletions packages/pyright-internal/src/tests/samples/overloadCall5.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@
# This should generate an error because this overload overlaps
# with the third one and returns a different type.
@overload
def func1(__iter1: Iterable[_T1]) -> Tuple[_T1]:
...
def func1(__iter1: Iterable[_T1]) -> Tuple[_T1]: ...


@overload
def func1(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> Tuple[_T1, _T2]:
...
def func1(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> Tuple[_T1, _T2]: ...


@overload
def func1(*iterables: Iterable[_T1]) -> float:
...
def func1(*iterables: Iterable[_T1]) -> float: ...


def func1(*iterables: Iterable[_T1 | _T2]) -> Tuple[_T1 | _T2, ...] | float:
...
def func1(*iterables: Iterable[_T1 | _T2]) -> Tuple[_T1 | _T2, ...] | float: ...


def test1(x: Iterable[int]):
Expand All @@ -48,18 +44,15 @@ def test1(x: Iterable[int]):


@overload
def func2() -> tuple[()]:
...
def func2() -> tuple[()]: ...


@overload
def func2(x: int, /) -> tuple[int]:
...
def func2(x: int, /) -> tuple[int]: ...


@overload
def func2(*x: int) -> tuple[int, ...]:
...
def func2(*x: int) -> tuple[int, ...]: ...


def func2(*x: int) -> tuple[int, ...]:
Expand All @@ -70,3 +63,20 @@ def func2(*x: int) -> tuple[int, ...]:
reveal_type(func2(1), expected_text="tuple[int]")
reveal_type(func2(1, 2), expected_text="tuple[int, ...]")
reveal_type(func2(*[1, 2, 3]), expected_text="tuple[int, ...]")


@overload
def func3(x: int, /) -> str: ...


@overload
def func3(x: int, y: int, /, *args: int) -> int: ...


def func3(*args: int) -> int | str:
return 1


def test3(v: list[int]) -> None:
r = func3(*v)
reveal_type(r, expected_text="int")

0 comments on commit 9295635

Please sign in to comment.