Skip to content

Commit ffeee66

Browse files
authored
[ty] Unions/intersections of gradual types should be assignable to Never (#24056)
This showed up in #23761 with `Divergent | Any`.
1 parent 632fde9 commit ffeee66

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,24 @@ static_assert(is_assignable_to(Never, type[str]))
924924
static_assert(is_assignable_to(Never, type[Any]))
925925
```
926926

927+
### `Any` / `Unknown` is assignable to `Never`
928+
929+
`Any` and `Unknown` are gradual types. They could materialize to any given type at runtime,
930+
including `Never`.
931+
932+
```py
933+
from ty_extensions import static_assert, is_assignable_to, Unknown, Intersection
934+
from typing_extensions import Never, Any
935+
936+
static_assert(is_assignable_to(Any, Never))
937+
static_assert(is_assignable_to(Unknown, Never))
938+
static_assert(is_assignable_to(Any | Unknown, Never))
939+
static_assert(is_assignable_to(Intersection[Any, int], Never))
940+
static_assert(is_assignable_to(Intersection[Unknown, int], Never))
941+
static_assert(not is_assignable_to(Any | int, Never))
942+
static_assert(not is_assignable_to(Unknown | int, Never))
943+
```
944+
927945
## Callable
928946

929947
The examples provided below are only a subset of the possible cases and include the ones with

crates/ty_python_semantic/src/types/relation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,9 +897,6 @@ impl<'a, 'c, 'db> TypeRelationChecker<'a, 'c, 'db> {
897897
self.never()
898898
}
899899

900-
// `Never` is the bottom type, the empty set.
901-
(_, Type::Never) => self.never(),
902-
903900
(Type::NewTypeInstance(source_newtype), Type::NewTypeInstance(target_newtype)) => {
904901
self.check_newtype_pair(db, source_newtype, target_newtype)
905902
}
@@ -1027,6 +1024,9 @@ impl<'a, 'c, 'db> TypeRelationChecker<'a, 'c, 'db> {
10271024
)
10281025
}
10291026

1027+
// `Never` is the bottom type, the empty set.
1028+
(_, Type::Never) => self.never(),
1029+
10301030
// Other than the special cases checked above, no other types are a subtype of a
10311031
// typevar, since there's no guarantee what type the typevar will be specialized to.
10321032
// (If the typevar is bounded, it might be specialized to a smaller type than the

0 commit comments

Comments
 (0)