Modified previous heuristic change so it applies only in cases where the source function has a declared return type with a literal annotation in it. It doesn't apply in cases involving an inferred return type or generic types that are specialized with literals.

This commit is contained in:
Eric Traut 2022-05-05 21:09:29 -07:00
parent dd7e3b2b82
commit 5bbb3b5eb8
2 changed files with 20 additions and 1 deletions

View File

@ -22651,6 +22651,18 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
let isReturnTypeCompatible = false;
let effectiveFlags = flags;
// If the source has a declared return type that includes a literal
// in its annotation, assume that we will want the constraint
// solver to retain literals.
if (
srcType.details.declaredReturnType &&
containsLiteralType(srcType.details.declaredReturnType, /* includeTypeArgs */ true)
) {
effectiveFlags |= CanAssignFlags.RetainLiteralsForTypeVar;
}
if (isNever(srcReturnType)) {
// We'll allow any function that returns NoReturn to match any
// function return type, consistent with other type checkers.
@ -22661,7 +22673,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
srcReturnType,
returnDiag?.createAddendum(),
typeVarContext,
flags | CanAssignFlags.RetainLiteralsForTypeVar,
effectiveFlags,
recursionCount
)
) {

View File

@ -27,3 +27,10 @@ def f2() -> Literal["Foo"]:
reveal_type(wrapper2(f2)(), expected_text="Literal['Foo']")
def f3():
return "Foo"
reveal_type(wrapper2(f3)(), expected_text="str")