Skip to content

Commit 1f380c8

Browse files
authored
[ty] Update tests reveal_type and Never (#23418)
## Summary `reveal_type(something_of_type_never)` is a call to a function that returns `Never`, since `reveal_type` returns the type of its argument. So it is conceivable that we treat it just like any other call to a terminal function in terms of control flow analysis. This change to our tests prepares us for that scenario in that it splits tests into multiple functions, so that assertions following a `reveal_type(something_of_type_never)` call can no longer be considered unreachable.
1 parent 6551438 commit 1f380c8

File tree

5 files changed

+77
-58
lines changed

5 files changed

+77
-58
lines changed

crates/ty_python_semantic/resources/mdtest/annotations/never.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ reveal_type(stop())
2222
from typing_extensions import NoReturn, Never, Any
2323

2424
# error: [invalid-type-form] "Type `typing.Never` expected no type parameter"
25-
x: Never[int]
26-
a1: NoReturn
27-
a2: Never
28-
b1: Any
29-
b2: int
25+
invalid: Never[int]
3026

31-
def f():
27+
def _(never: Never):
3228
# revealed: Never
33-
reveal_type(a1)
29+
reveal_type(never)
30+
31+
def _(noreturn: NoReturn):
3432
# revealed: Never
35-
reveal_type(a2)
36-
37-
# Never is assignable to all types.
38-
v1: int = a1
39-
v2: str = a1
40-
# Other types are not assignable to Never except for Never (and Any).
41-
v3: Never = b1
42-
v4: Never = a2
43-
v5: Any = b2
44-
# error: [invalid-assignment] "Object of type `Literal[1]` is not assignable to `Never`"
45-
v6: Never = 1
33+
reveal_type(noreturn)
34+
35+
# Never is assignable to all types:
36+
def _(never: Never):
37+
v1: int = never
38+
v2: str = never
39+
v3: Never = never
40+
v4: Any = never
41+
42+
# No type is assignable to Never except for Never (and Any):
43+
def _(never: Never, noreturn: NoReturn, any: Any):
44+
v1: Never = 1 # error: [invalid-assignment]
45+
v2: Never = "a" # error: [invalid-assignment]
46+
47+
v3: Never = any
48+
v4: Never = noreturn
49+
v4: NoReturn = never
4650
```
4751

4852
## `typing.Never`

crates/ty_python_semantic/resources/mdtest/attributes.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,11 +2125,12 @@ of type `Never`):
21252125
```py
21262126
from typing_extensions import Never, Any
21272127

2128-
def _(n: Never):
2129-
reveal_type(n.__setattr__) # revealed: Never
2128+
def _(never: Never):
2129+
reveal_type(never.__setattr__) # revealed: Never
21302130

2131+
def _(never: Never):
21312132
# No error:
2132-
n.non_existing = 1
2133+
never.non_existing = 1
21332134
```
21342135

21352136
And similarly for `Any`:

crates/ty_python_semantic/resources/mdtest/exhaustiveness_checking.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ def i[T: (int, str)](x: T) -> T:
471471
case str():
472472
pass
473473
case _:
474-
reveal_type(x) # revealed: Never
475474
assert_never(x)
476475

477476
return x

crates/ty_python_semantic/resources/mdtest/intersection_types.md

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -313,24 +313,29 @@ class P: ...
313313
class Q: ...
314314
class R(Generic[T_co]): ...
315315

316-
def _(
317-
i1: Intersection[P, Not[P]],
318-
i2: Intersection[Not[P], P],
319-
i3: Intersection[P, Q, Not[P]],
320-
i4: Intersection[Not[P], Q, P],
321-
i5: Intersection[P, Any, Not[P]],
322-
i6: Intersection[Not[P], Any, P],
323-
i7: Intersection[R[P], Not[R[P]]],
324-
i8: Intersection[R[P], Not[R[Q]]],
325-
) -> None:
326-
reveal_type(i1) # revealed: Never
327-
reveal_type(i2) # revealed: Never
328-
reveal_type(i3) # revealed: Never
329-
reveal_type(i4) # revealed: Never
330-
reveal_type(i5) # revealed: Never
331-
reveal_type(i6) # revealed: Never
332-
reveal_type(i7) # revealed: Never
333-
reveal_type(i8) # revealed: R[P] & ~R[Q]
316+
def _(i: Intersection[P, Not[P]]) -> None:
317+
reveal_type(i) # revealed: Never
318+
319+
def _(i: Intersection[Not[P], P]) -> None:
320+
reveal_type(i) # revealed: Never
321+
322+
def _(i: Intersection[P, Q, Not[P]]) -> None:
323+
reveal_type(i) # revealed: Never
324+
325+
def _(i: Intersection[Not[P], Q, P]) -> None:
326+
reveal_type(i) # revealed: Never
327+
328+
def _(i: Intersection[P, Any, Not[P]]) -> None:
329+
reveal_type(i) # revealed: Never
330+
331+
def _(i: Intersection[Not[P], Any, P]) -> None:
332+
reveal_type(i) # revealed: Never
333+
334+
def _(i: Intersection[R[P], Not[R[P]]]) -> None:
335+
reveal_type(i) # revealed: Never
336+
337+
def _(i: Intersection[R[P], Not[R[Q]]]) -> None:
338+
reveal_type(i) # revealed: R[P] & ~R[Q]
334339
```
335340

336341
### Union of a type and its negation
@@ -784,41 +789,49 @@ type Red = Literal[Color.RED]
784789
type Green = Literal[Color.GREEN]
785790
type Blue = Literal[Color.BLUE]
786791

787-
def f(
788-
a: Intersection[Color, Red],
789-
b: Intersection[Color, Not[Red]],
790-
c: Intersection[Color, Not[Red | Green]],
791-
d: Intersection[Color, Not[Red | Green | Blue]],
792-
e: Intersection[Red, Not[Color]],
793-
f: Intersection[Red | Green, Not[Color]],
794-
g: Intersection[Not[Red], Color],
795-
h: Intersection[Red, Green],
796-
i: Intersection[Red | Green, Green | Blue],
797-
):
792+
def _(a: Intersection[Color, Red]) -> None:
798793
reveal_type(a) # revealed: Literal[Color.RED]
794+
795+
def _(b: Intersection[Color, Not[Red]]) -> None:
799796
reveal_type(b) # revealed: Literal[Color.GREEN, Color.BLUE]
797+
798+
def _(c: Intersection[Color, Not[Red | Green]]) -> None:
800799
reveal_type(c) # revealed: Literal[Color.BLUE]
800+
801+
def _(d: Intersection[Color, Not[Red | Green | Blue]]) -> None:
801802
reveal_type(d) # revealed: Never
803+
804+
def _(e: Intersection[Red, Not[Color]]) -> None:
802805
reveal_type(e) # revealed: Never
806+
807+
def _(f: Intersection[Red | Green, Not[Color]]) -> None:
803808
reveal_type(f) # revealed: Never
809+
810+
def _(g: Intersection[Not[Red], Color]) -> None:
804811
reveal_type(g) # revealed: Literal[Color.GREEN, Color.BLUE]
812+
813+
def _(h: Intersection[Red, Green]) -> None:
805814
reveal_type(h) # revealed: Never
815+
816+
def _(i: Intersection[Red | Green, Green | Blue]) -> None:
806817
reveal_type(i) # revealed: Literal[Color.GREEN]
807818

808819
class Single(Enum):
809820
VALUE = 0
810821

811-
def g(
812-
a: Intersection[Single, Literal[Single.VALUE]],
813-
b: Intersection[Single, Not[Literal[Single.VALUE]]],
814-
c: Intersection[Not[Literal[Single.VALUE]], Single],
815-
d: Intersection[Single, Not[Single]],
816-
e: Intersection[Single | int, Not[Single]],
817-
):
822+
def _(a: Intersection[Single, Literal[Single.VALUE]]) -> None:
818823
reveal_type(a) # revealed: Single
824+
825+
def _(b: Intersection[Single, Not[Literal[Single.VALUE]]]) -> None:
819826
reveal_type(b) # revealed: Never
827+
828+
def _(c: Intersection[Not[Literal[Single.VALUE]], Single]) -> None:
820829
reveal_type(c) # revealed: Never
830+
831+
def _(d: Intersection[Single, Not[Single]]) -> None:
821832
reveal_type(d) # revealed: Never
833+
834+
def _(e: Intersection[Single | int, Not[Single]]) -> None:
822835
reveal_type(e) # revealed: int
823836
```
824837

crates/ty_python_semantic/resources/mdtest/narrow/match.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,14 @@ def _(x: A | B | C):
228228
case _:
229229
reveal_type(x) # revealed: Never
230230

231+
def _(x: A | B | C):
231232
match x:
232233
case A() | B() | C():
233234
reveal_type(x) # revealed: A | B | C
234235
case _:
235236
reveal_type(x) # revealed: Never
236237

238+
def _(x: A | B | C):
237239
match x:
238240
case A():
239241
reveal_type(x) # revealed: A

0 commit comments

Comments
 (0)