Skip to content

Commit

Permalink
Changed behavior to allow a value with type _AnnotatedAlias to be u…
Browse files Browse the repository at this point in the history
…sed with an implicit `__getitem__` call even though this type is not documented or declared in typeshed. This addresses #8916.
  • Loading branch information
erictraut committed Sep 6, 2024
1 parent 05435ba commit 8191861
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/tests/samples/annotated1.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def func2(a: TypeWithStringArg):
Param = Annotated[_T, "x"]

x1: Param[int] = 3
print(Param[int])


class A:
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/typeAlias1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/typeForm4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8191861

Please sign in to comment.