Skip to content

Commit b4208f6

Browse files
committed
Handle PEP 613 aliases as well
1 parent 4f6d9af commit b4208f6

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

crates/ty_python_semantic/resources/mdtest/pep613_type_aliases.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ def _(x: MyAlias):
9696
reveal_type(x) # revealed: int | ((str, /) -> int)
9797
```
9898

99+
## Generic aliases
100+
101+
```py
102+
from typing import TypeAlias, TypeVar
103+
104+
T = TypeVar("T")
105+
106+
MyList: TypeAlias = list[T]
107+
ListOrSet: TypeAlias = list[T] | set[T]
108+
109+
reveal_type(MyList) # revealed: <class 'list[T]'>
110+
reveal_type(ListOrSet) # revealed: types.UnionType
111+
112+
def _(list_of_int: MyList[int], list_or_set_of_str: ListOrSet[str]):
113+
reveal_type(list_of_int) # revealed: list[int]
114+
reveal_type(list_or_set_of_str) # revealed: list[str] | set[str]
115+
```
116+
99117
## Subscripted generic alias in union
100118

101119
```py

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5531,11 +5531,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
55315531
self.deferred_state = DeferredExpressionState::Deferred;
55325532
}
55335533

5534+
let previous_typevar_binding_context = self.typevar_binding_context;
5535+
self.typevar_binding_context = Some(definition);
5536+
55345537
let inferred_ty = self.infer_maybe_standalone_expression(
55355538
value,
55365539
TypeContext::new(Some(declared.inner_type())),
55375540
);
55385541

5542+
self.typevar_binding_context = previous_typevar_binding_context;
5543+
55395544
self.deferred_state = previous_deferred_state;
55405545

55415546
self.dataclass_field_specifiers.clear();

0 commit comments

Comments
 (0)