-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug that led to incorrect type evaluation when an inferred me…
…thod return type includes a union where the subtypes are conditioned on constraints of a constrained TypeVar that parameterizes the class. In this case, one or more of these subtypes should be eliminated when a specialized class is bound to the method. This addresses #6446. (#6449)
- Loading branch information
Showing
6 changed files
with
114 additions
and
5 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
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
38 changes: 38 additions & 0 deletions
38
packages/pyright-internal/src/tests/samples/constrainedTypeVar18.py
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,38 @@ | ||
# This sample tests the case where an inferred method return type is | ||
# a union with subtypes that are conditioned on different constraints of | ||
# a constrained TypeVar. When the method is bound, one or more of these | ||
# subtypes should be eliminated. | ||
|
||
from typing import Generic, TypeVar, Awaitable | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Async: | ||
def fn(self, returnable: T) -> Awaitable[T]: | ||
... | ||
|
||
|
||
class Sync: | ||
def fn(self, returnable: T) -> T: | ||
... | ||
|
||
|
||
T = TypeVar("T", Async, Sync) | ||
|
||
|
||
class A(Generic[T]): | ||
def __init__(self, client: T): | ||
self._client = client | ||
|
||
def method1(self): | ||
return self._client.fn(7) | ||
|
||
|
||
a1 = A(Async()) | ||
r1 = a1.method1() | ||
reveal_type(r1, expected_text="Awaitable[int]*") | ||
|
||
a2 = A(Sync()) | ||
r2 = a2.method1() | ||
reveal_type(r2, expected_text="int*") |
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