-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue in type evaluation of call expressions where the calla…
…ble subexpression evaluates to a union, and the callable subtypes have different signatures. Pyright was previously caching the types from the first subtype, so it didn't re-evaluate using the second subtype (which may require bidirectional type inference). This addresses #5428. (#5547) Co-authored-by: Eric Traut <[email protected]>
- Loading branch information
1 parent
98fcba1
commit 9aa29e4
Showing
4 changed files
with
127 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This sample tests the case where a call expression involves a union | ||
# on the LHS where the subtypes of the union have different signatures. | ||
|
||
# pyright: strict | ||
|
||
from __future__ import annotations | ||
from typing import Any, Callable, Generic, Self, TypeAlias, TypeVar | ||
|
||
T = TypeVar("T") | ||
E = TypeVar("E") | ||
U = TypeVar("U") | ||
F = TypeVar("F") | ||
|
||
Either: TypeAlias = "Left[T]" | "Right[E]" | ||
|
||
|
||
class Left(Generic[T]): | ||
def __init__(self, value: T) -> None: | ||
self.value = value | ||
|
||
def map_left(self, fn: Callable[[T], U]) -> Left[U]: | ||
return Left(fn(self.value)) | ||
|
||
def map_right(self, fn: Callable[[Any], Any]) -> Self: | ||
return self | ||
|
||
|
||
class Right(Generic[E]): | ||
def __init__(self, value: E) -> None: | ||
self.value = value | ||
|
||
def map_left(self, fn: Callable[[Any], Any]) -> Self: | ||
return self | ||
|
||
def map_right(self, fn: Callable[[E], F]) -> Right[F]: | ||
return Right(fn(self.value)) | ||
|
||
|
||
def func() -> Either[int, str]: | ||
raise NotImplementedError | ||
|
||
|
||
result = func().map_left(lambda lv: lv + 1).map_right(lambda rv: rv + "a") | ||
reveal_type(result, expected_text="Left[int] | Right[str]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters