From 819186184014e4bde68572360231f2046c87957d Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 6 Sep 2024 11:18:28 -0700 Subject: [PATCH] Changed behavior to allow a value with type `_AnnotatedAlias` to be used with an implicit `__getitem__` call even though this type is not documented or declared in typeshed. This addresses #8916. --- packages/pyright-internal/src/analyzer/typeEvaluator.ts | 8 ++++++++ packages/pyright-internal/src/tests/samples/annotated1.py | 1 + packages/pyright-internal/src/tests/samples/typeAlias1.py | 2 +- packages/pyright-internal/src/tests/samples/typeForm4.py | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index 965dda1bc589..38ce7af828b9 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -4776,6 +4776,14 @@ export function createTypeEvaluator( return type; } + // If this is a type alias and we are not supposed to specialize it, return it as is. + if ((flags & EvalFlags.NoSpecialize) !== 0 && type.props?.typeAliasInfo) { + // Special-case TypeAliasType which should be converted in this case. + if (!ClassType.isBuiltIn(type.props.specialForm, 'TypeAliasType')) { + return type; + } + } + if (type.props?.typeForm) { return TypeBase.cloneWithTypeForm(type.props.specialForm, type.props.typeForm); } diff --git a/packages/pyright-internal/src/tests/samples/annotated1.py b/packages/pyright-internal/src/tests/samples/annotated1.py index 7014ea361a80..98c088ed33eb 100644 --- a/packages/pyright-internal/src/tests/samples/annotated1.py +++ b/packages/pyright-internal/src/tests/samples/annotated1.py @@ -54,6 +54,7 @@ def func2(a: TypeWithStringArg): Param = Annotated[_T, "x"] x1: Param[int] = 3 +print(Param[int]) class A: diff --git a/packages/pyright-internal/src/tests/samples/typeAlias1.py b/packages/pyright-internal/src/tests/samples/typeAlias1.py index 2328686cd05e..57df5ff52be2 100644 --- a/packages/pyright-internal/src/tests/samples/typeAlias1.py +++ b/packages/pyright-internal/src/tests/samples/typeAlias1.py @@ -23,7 +23,7 @@ class A: Value2 = 1 -reveal_type(A.Value1, expected_text="type[Literal]") +reveal_type(A.Value1, expected_text="type[Literal[1]]") reveal_type(A.Value2, expected_text="int") diff --git a/packages/pyright-internal/src/tests/samples/typeForm4.py b/packages/pyright-internal/src/tests/samples/typeForm4.py index 682b9deb5e08..297faf7bcc5a 100644 --- a/packages/pyright-internal/src/tests/samples/typeForm4.py +++ b/packages/pyright-internal/src/tests/samples/typeForm4.py @@ -48,7 +48,7 @@ def func1(): t10: TypeForm = TA2[str] t11: TypeForm[TA2[str]] = TA2[str] - t12: TypeForm[list[Any]] = TA2[str] + t12: TypeForm[list[Any] | str] = TA2[str] t13: TypeForm = TA2