Improved type printer (the component that converts an internal type to textual format for diagnostic messages) so it better handles special forms like Literal or unions when used in a value expression. This partly addresses #7547.

This commit is contained in:
Eric Traut 2024-05-21 10:00:42 -07:00
parent 1027400ac0
commit 4c49f1b8cd
2 changed files with 18 additions and 1 deletions

View File

@ -450,7 +450,7 @@ function printTypeInternal(
}
} else {
typeToWrap = printObjectTypeForClassInternal(
type,
type.specialForm ?? type,
printTypeFlags,
returnTypeCallback,
uniqueNameMap,
@ -510,6 +510,19 @@ function printTypeInternal(
}
case TypeCategory.Union: {
// If this is a value expression that evaluates to a union type but is
// not a type alias, simply print the special form ("UnionType").
if (TypeBase.isInstantiable(type) && type.specialForm && !type.typeAliasInfo) {
return printTypeInternal(
type.specialForm,
printTypeFlags,
returnTypeCallback,
uniqueNameMap,
recursionTypes,
recursionCount
);
}
// Allocate a set that refers to subtypes in the union by
// their indices. If the index is within the set, it is already
// accounted for in the output.

View File

@ -56,3 +56,7 @@ bytes3 = b'"'
reveal_type(bytes3, expected_text='Literal[b"\\""]')
bytes4 = b"'"
reveal_type(bytes4, expected_text='Literal[b"\'"]')
x = [Literal[1], Literal[2]]
reveal_type(x, expected_text="list[type[Literal]]")