Changed the heuristics in the TypeVar constraint solver to preserve literals when solving for a TypeVar in a Callable return type.

This commit is contained in:
Eric Traut 2022-05-05 21:00:56 -07:00
parent 648ae44bfe
commit dd7e3b2b82
2 changed files with 15 additions and 4 deletions

View File

@ -22661,7 +22661,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
srcReturnType,
returnDiag?.createAddendum(),
typeVarContext,
flags,
flags | CanAssignFlags.RetainLiteralsForTypeVar,
recursionCount
)
) {

View File

@ -7,12 +7,23 @@ from typing import Callable, TypeVar, Literal
_A = TypeVar("_A")
def wrapper(fn: Callable[[_A], int]) -> _A:
def wrapper1(fn: Callable[[_A], int]) -> _A:
...
def f3(a: Literal[0]) -> int:
def f1(a: Literal[0]) -> int:
...
wrapper(f3)
reveal_type(wrapper1(f1), expected_text="Literal[0]")
def wrapper2(fn: Callable[..., _A]) -> Callable[..., _A]:
...
def f2() -> Literal["Foo"]:
return "Foo"
reveal_type(wrapper2(f2)(), expected_text="Literal['Foo']")