Skip to content

Commit 80eec96

Browse files
authored
Add PosArgT typing to trio.run (#3022)
* add PosArgT typing to run() * add type tests
1 parent a7db0e4 commit 80eec96

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/trio/_core/_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,8 +2184,8 @@ def setup_runner(
21842184

21852185

21862186
def run(
2187-
async_fn: Callable[..., Awaitable[RetT]],
2188-
*args: object,
2187+
async_fn: Callable[[Unpack[PosArgT]], Awaitable[RetT]],
2188+
*args: Unpack[PosArgT],
21892189
clock: Clock | None = None,
21902190
instruments: Sequence[Instrument] = (),
21912191
restrict_keyboard_interrupt_to_checkpoints: bool = False,

src/trio/_core/_tests/test_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def trivial(x: T) -> T:
7676

7777
with pytest.raises(TypeError):
7878
# Missing an argument
79-
_core.run(trivial)
79+
_core.run(trivial) # type: ignore[arg-type]
8080

8181
with pytest.raises(TypeError):
8282
# Not an async function
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import annotations
2+
3+
from typing import Sequence, overload
4+
5+
import trio
6+
from typing_extensions import assert_type
7+
8+
9+
async def sleep_sort(values: Sequence[float]) -> list[float]:
10+
return [1]
11+
12+
13+
async def has_optional(arg: int | None = None) -> int:
14+
return 5
15+
16+
17+
@overload
18+
async def foo_overloaded(arg: int) -> str: ...
19+
20+
21+
@overload
22+
async def foo_overloaded(arg: str) -> int: ...
23+
24+
25+
async def foo_overloaded(arg: int | str) -> int | str:
26+
if isinstance(arg, str):
27+
return 5
28+
return "hello"
29+
30+
31+
v = trio.run(
32+
sleep_sort, (1, 3, 5, 2, 4), clock=trio.testing.MockClock(autojump_threshold=0)
33+
)
34+
assert_type(v, "list[float]")
35+
trio.run(sleep_sort, ["hi", "there"]) # type: ignore[arg-type]
36+
trio.run(sleep_sort) # type: ignore[arg-type]
37+
38+
r = trio.run(has_optional)
39+
assert_type(r, int)
40+
r = trio.run(has_optional, 5)
41+
trio.run(has_optional, 7, 8) # type: ignore[arg-type]
42+
trio.run(has_optional, "hello") # type: ignore[arg-type]
43+
44+
45+
assert_type(trio.run(foo_overloaded, 5), str)
46+
assert_type(trio.run(foo_overloaded, ""), int)

0 commit comments

Comments
 (0)