Skip to content

Commit d899f83

Browse files
committed
prevent incorrect specializations in SpecializationBuilder::infer
1 parent 9622841 commit d899f83

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/ty_python_semantic/resources/mdtest/bidirectional.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def _(l: list[int] | None = None):
4646
l2: list[int] = l or list()
4747
# TODO: should be `list[int]`
4848
reveal_type(l2) # revealed: (list[int] & ~AlwaysFalsy) | list[Unknown]
49+
50+
def f[T](x: T, cond: bool) -> T | list[T]:
51+
return x if cond else [x]
52+
53+
# TODO: no error
54+
# error: [invalid-assignment] "Object of type `Literal[1] | list[Literal[1]]` is not assignable to `int | list[int]`"
55+
t: int | list[int] = f(1, True)
4956
```
5057

5158
```py
@@ -123,4 +130,10 @@ reveal_type(f("a")) # revealed: list[str]
123130

124131
async def g() -> list[int]:
125132
return list1(1)
133+
134+
def h[T](x: T, cond: bool) -> T | list[T]:
135+
return i(x, cond)
136+
137+
def i[T](x: T, cond: bool) -> T | list[T]:
138+
return x if cond else [x]
126139
```

crates/ty_python_semantic/src/types/generics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,11 @@ impl<'db> SpecializationBuilder<'db> {
10881088
}
10891089

10901090
match (formal, actual) {
1091+
(Type::Union(_), Type::Union(_)) => {
1092+
// TODO: We need to infer specializations appropriately.
1093+
// e.g.
1094+
// `formal: list[T] | T | U, actual: V | int | list[V]` => `T = V, U = int`
1095+
}
10911096
(Type::Union(formal), _) => {
10921097
// TODO: We haven't implemented a full unification solver yet. If typevars appear
10931098
// in multiple union elements, we ideally want to express that _only one_ of them

0 commit comments

Comments
 (0)