mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
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:
parent
1027400ac0
commit
4c49f1b8cd
@ -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.
|
||||
|
@ -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]]")
|
||||
|
Loading…
Reference in New Issue
Block a user