Skip to content

Commit c628e3e

Browse files
authored
[ty] Prepare test files for reachability changes (astral-sh#24207)
## Summary Some mdtest changes in preparation for astral-sh#23245.
1 parent 207232c commit c628e3e

4 files changed

Lines changed: 63 additions & 53 deletions

File tree

crates/ty_python_semantic/resources/mdtest/call/getattr_static.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ When a non-existent attribute is accessed without a default value, the runtime r
4747
`AttributeError`. We could emit a diagnostic for this case, but that is currently not supported:
4848

4949
```py
50-
# TODO: we could emit a diagnostic here
51-
reveal_type(inspect.getattr_static(C, "non_existent")) # revealed: Never
50+
def _():
51+
# TODO: we could emit a diagnostic here
52+
reveal_type(inspect.getattr_static(C, "non_existent")) # revealed: Never
5253
```
5354

5455
We can access attributes on objects of all kinds:

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,24 +294,16 @@ def _(x: Literal["foo", b"bar"] | int):
294294
## Narrowing due to guard
295295

296296
```py
297-
def get_object() -> object:
298-
return object()
299-
300-
x = get_object()
301-
302-
reveal_type(x) # revealed: object
303-
304-
match x:
305-
case str() | float() if type(x) is str:
306-
reveal_type(x) # revealed: str
307-
case "foo" | 42 | None if isinstance(x, int):
308-
reveal_type(x) # revealed: int
309-
case False if x:
310-
reveal_type(x) # revealed: Never
311-
case "foo" if x := "bar":
312-
reveal_type(x) # revealed: Literal["bar"]
313-
314-
reveal_type(x) # revealed: object
297+
def _(x: object):
298+
match x:
299+
case str() | float() if type(x) is str:
300+
reveal_type(x) # revealed: str
301+
case "foo" | 42 | None if isinstance(x, int):
302+
reveal_type(x) # revealed: int
303+
case False if x:
304+
reveal_type(x) # revealed: Never
305+
case "foo" if x := "bar":
306+
reveal_type(x) # revealed: Literal["bar"]
315307
```
316308

317309
## Guard and reveal_type in guard

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,16 @@ async def main(val: int | None):
256256
await stop()
257257
reveal_type(val) # revealed: int
258258
```
259+
260+
## Narrowing in global scope
261+
262+
```py
263+
data: dict[str, str] = {}
264+
api_key = data.get("api_key")
265+
266+
if not api_key:
267+
exit(1)
268+
269+
# TODO: Should be str & ~AlwaysFalsy
270+
reveal_type(api_key) # revealed: str | None
271+
```

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

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,51 @@
33
## Value Literals
44

55
```py
6-
from typing import Literal
7-
8-
def foo() -> Literal[0, -1, True, False, "", "foo", b"", b"bar", None] | tuple[()]:
9-
return 0
6+
from typing import Literal, TypeAlias
107

11-
x = foo()
8+
X: TypeAlias = Literal[0, -1, True, False, "", "foo", b"", b"bar", None] | tuple[()]
129

13-
if x:
14-
reveal_type(x) # revealed: Literal[-1, True, "foo", b"bar"]
15-
else:
16-
reveal_type(x) # revealed: Literal[0, False, "", b""] | None | tuple[()]
10+
def _(x: X):
11+
if x:
12+
reveal_type(x) # revealed: Literal[-1, True, "foo", b"bar"]
13+
else:
14+
reveal_type(x) # revealed: Literal[0, False, "", b""] | None | tuple[()]
1715

18-
if not x:
19-
reveal_type(x) # revealed: Literal[0, False, "", b""] | None | tuple[()]
20-
else:
21-
reveal_type(x) # revealed: Literal[-1, True, "foo", b"bar"]
16+
def _(x: X):
17+
if not x:
18+
reveal_type(x) # revealed: Literal[0, False, "", b""] | None | tuple[()]
19+
else:
20+
reveal_type(x) # revealed: Literal[-1, True, "foo", b"bar"]
2221

23-
if x and not x:
24-
reveal_type(x) # revealed: Never
25-
else:
26-
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
22+
def _(x: X):
23+
if x and not x:
24+
reveal_type(x) # revealed: Never
25+
else:
26+
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
2727

28-
if not (x and not x):
29-
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
30-
else:
31-
reveal_type(x) # revealed: Never
28+
def _(x: X):
29+
if not (x and not x):
30+
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
31+
else:
32+
reveal_type(x) # revealed: Never
3233

33-
if x or not x:
34-
reveal_type(x) # revealed: Literal[-1, 0, "foo", "", b"bar", b""] | bool | None | tuple[()]
35-
else:
36-
reveal_type(x) # revealed: Never
34+
def _(x: X):
35+
if x or not x:
36+
reveal_type(x) # revealed: Literal[-1, 0, "foo", "", b"bar", b""] | bool | None | tuple[()]
37+
else:
38+
reveal_type(x) # revealed: Never
3739

38-
if not (x or not x):
39-
reveal_type(x) # revealed: Never
40-
else:
41-
reveal_type(x) # revealed: Literal[-1, 0, "foo", "", b"bar", b""] | bool | None | tuple[()]
40+
def _(x: X):
41+
if not (x or not x):
42+
reveal_type(x) # revealed: Never
43+
else:
44+
reveal_type(x) # revealed: Literal[-1, 0, "foo", "", b"bar", b""] | bool | None | tuple[()]
4245

43-
if (isinstance(x, int) or isinstance(x, str)) and x:
44-
reveal_type(x) # revealed: Literal[-1, True, "foo"]
45-
else:
46-
reveal_type(x) # revealed: Literal[b"", b"bar", 0, False, ""] | None | tuple[()]
46+
def _(x: X):
47+
if (isinstance(x, int) or isinstance(x, str)) and x:
48+
reveal_type(x) # revealed: Literal[-1, True, "foo"]
49+
else:
50+
reveal_type(x) # revealed: Literal[b"", b"bar", 0, False, ""] | None | tuple[()]
4751
```
4852

4953
## Walrus Member Access

0 commit comments

Comments
 (0)