Skip to content

Commit c874638

Browse files
sharkdpAlexWaygood
andauthored
[red-knot] Move tuple-containing-Never tests to Markdown (#15402)
## Summary See title. Part of #15397 ## Test Plan Ran new Markdown test. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent c364b58 commit c874638

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Tuples containing `Never`
2+
3+
A heterogeneous `tuple[…]` type that contains `Never` as a type argument simplifies to `Never`. One
4+
way to think about this is the following: in order to construct a tuple, you need to have an object
5+
of every element type. But since there is no object of type `Never`, you cannot construct the tuple.
6+
Such a tuple type is therefore uninhabited and equivalent to `Never`.
7+
8+
In the language of algebraic data types, a tuple type is a product type and `Never` acts like the
9+
zero element in multiplication, similar to how a Cartesian product with the empty set is the empty
10+
set.
11+
12+
```py
13+
from knot_extensions import static_assert, is_equivalent_to
14+
from typing_extensions import Never, NoReturn
15+
16+
static_assert(is_equivalent_to(Never, tuple[Never]))
17+
static_assert(is_equivalent_to(Never, tuple[Never, int]))
18+
static_assert(is_equivalent_to(Never, tuple[int, Never]))
19+
static_assert(is_equivalent_to(Never, tuple[int, Never, str]))
20+
static_assert(is_equivalent_to(Never, tuple[int, tuple[str, Never]]))
21+
static_assert(is_equivalent_to(Never, tuple[tuple[str, Never], int]))
22+
23+
# The empty tuple is *not* equivalent to Never!
24+
static_assert(not is_equivalent_to(Never, tuple[()]))
25+
26+
# NoReturn is just a different spelling of Never, so the same is true for NoReturn
27+
static_assert(is_equivalent_to(NoReturn, tuple[NoReturn]))
28+
static_assert(is_equivalent_to(NoReturn, tuple[NoReturn, int]))
29+
static_assert(is_equivalent_to(NoReturn, tuple[int, NoReturn]))
30+
static_assert(is_equivalent_to(NoReturn, tuple[int, NoReturn, str]))
31+
static_assert(is_equivalent_to(NoReturn, tuple[int, tuple[str, NoReturn]]))
32+
static_assert(is_equivalent_to(NoReturn, tuple[tuple[str, NoReturn], int]))
33+
```

crates/red_knot_python_semantic/src/types.rs

-8
Original file line numberDiff line numberDiff line change
@@ -4144,14 +4144,6 @@ pub(crate) mod tests {
41444144
}
41454145
}
41464146

4147-
#[test_case(Ty::Tuple(vec![Ty::Never]))]
4148-
#[test_case(Ty::Tuple(vec![Ty::BuiltinInstance("str"), Ty::Never, Ty::BuiltinInstance("int")]))]
4149-
#[test_case(Ty::Tuple(vec![Ty::Tuple(vec![Ty::Never])]))]
4150-
fn tuple_containing_never_simplifies_to_never(ty: Ty) {
4151-
let db = setup_db();
4152-
assert_eq!(ty.into_type(&db), Type::Never);
4153-
}
4154-
41554147
#[test_case(Ty::BuiltinInstance("str"), Ty::BuiltinInstance("object"))]
41564148
#[test_case(Ty::BuiltinInstance("int"), Ty::BuiltinInstance("object"))]
41574149
#[test_case(Ty::BuiltinInstance("bool"), Ty::BuiltinInstance("object"))]

0 commit comments

Comments
 (0)