-
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.
Added heuristic to constraint solver logic to better handle the case …
…where a parameter is annotated with a union of multiple "bare" TypeVars (like `S | T`). In this case, it's not clear whether the corresponding argument type should constrain `S` or `T` or both. We now defer the constraint during the first pass of arg/param type validation so additional references to `S` or `T` can help establish the appropriate constraint. This addresses #5768. (#5772) Co-authored-by: Eric Traut <[email protected]>
- Loading branch information
1 parent
e2cf85f
commit 2a42d4b
Showing
3 changed files
with
69 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This sample tests the case where a union of two "bare" TypeVars are | ||
# used in the annotation of a function parameter. | ||
|
||
from typing import Any, TypeVar, Callable | ||
|
||
S = TypeVar("S") | ||
T = TypeVar("T") | ||
|
||
|
||
def accepts_bool(b: bool) -> None: | ||
... | ||
|
||
|
||
def accepts_int(i: int) -> None: | ||
... | ||
|
||
|
||
def func1(x: S | T, l2: Callable[[S], Any], l3: Callable[[T], Any]) -> tuple[S, T]: | ||
... | ||
|
||
|
||
def func2(x: T | S, l2: Callable[[S], Any], l3: Callable[[T], Any]) -> tuple[S, T]: | ||
... | ||
|
||
|
||
x1 = func1(0, accepts_int, accepts_bool) | ||
reveal_type(x1, expected_text="tuple[int, bool]") | ||
|
||
x2 = func1(True, accepts_int, accepts_bool) | ||
reveal_type(x2, expected_text="tuple[int, bool]") | ||
|
||
x3 = func1(0, accepts_int, accepts_bool) | ||
reveal_type(x3, expected_text="tuple[int, bool]") | ||
|
||
x4 = func1(True, accepts_int, accepts_bool) | ||
reveal_type(x4, expected_text="tuple[int, bool]") |
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