Skip to content

Commit a1e4916

Browse files
CoolCat467jakkdl
andcommitted
Apply requested changes from code review
Co-authored-by: John Litborn <[email protected]>
1 parent 28b4abc commit a1e4916

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

src/trio/_core/_io_windows.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from contextlib import contextmanager
88
from typing import (
99
TYPE_CHECKING,
10-
Any,
1110
Literal,
11+
Protocol,
1212
TypeVar,
1313
cast,
1414
)
@@ -249,14 +249,26 @@ class AFDWaiters:
249249
current_op: AFDPollOp | None = None
250250

251251

252+
class AFDHandle(Protocol):
253+
Handle: Handle
254+
Status: int
255+
Events: int
256+
257+
258+
class AFDPollInfo(Protocol):
259+
Timeout: int
260+
NumberOfHandles: int
261+
Exclusive: int
262+
Handles: list[AFDHandle]
263+
264+
252265
# We also need to bundle up all the info for a single op into a standalone
253266
# object, because we need to keep all these objects alive until the operation
254267
# finishes, even if we're throwing it away.
255268
@attrs.frozen(eq=False)
256-
# Explicit "Any" is not allowed
257-
class AFDPollOp: # type: ignore[misc]
269+
class AFDPollOp:
258270
lpOverlapped: CData
259-
poll_info: Any # type: ignore[misc]
271+
poll_info: AFDPollInfo
260272
waiters: AFDWaiters
261273
afd_group: AFDGroup
262274

@@ -685,8 +697,7 @@ def _refresh_afd(self, base_handle: Handle) -> None:
685697

686698
lpOverlapped = ffi.new("LPOVERLAPPED")
687699

688-
# Explicit "Any" is not allowed
689-
poll_info: Any = ffi.new("AFD_POLL_INFO *") # type: ignore[misc]
700+
poll_info: AFDPollInfo = ffi.new("AFD_POLL_INFO *")
690701
poll_info.Timeout = 2**63 - 1 # INT64_MAX
691702
poll_info.NumberOfHandles = 1
692703
poll_info.Exclusive = 0

src/trio/_core/_run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,6 @@ def print_stack_for_task(task):
14751475
14761476
"""
14771477
# Ignore static typing as we're doing lots of dynamic introspection
1478-
# Explicit "Any" is not allowed
14791478
coro: Any = self.coro # type: ignore[misc]
14801479
while coro is not None:
14811480
if hasattr(coro, "cr_frame"):

src/trio/_core/_tests/test_run.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import weakref
1111
from contextlib import ExitStack, contextmanager, suppress
1212
from math import inf, nan
13-
from typing import TYPE_CHECKING, Any, NoReturn, TypeVar, cast
13+
from typing import TYPE_CHECKING, NoReturn, TypeVar
1414

1515
import outcome
1616
import pytest
@@ -2295,7 +2295,8 @@ async def detachable_coroutine(
22952295
await sleep(0)
22962296
nonlocal task, pdco_outcome
22972297
task = _core.current_task()
2298-
pdco_outcome = await outcome.acapture(
2298+
# `No overload variant of "acapture" matches argument types "Callable[[Outcome[object]], Coroutine[Any, Any, object]]", "Outcome[None]"`
2299+
pdco_outcome = await outcome.acapture( # type: ignore[call-overload]
22992300
_core.permanently_detach_coroutine_object,
23002301
task_outcome,
23012302
)
@@ -2308,18 +2309,11 @@ async def detachable_coroutine(
23082309
# is still iterable. At that point anything can be sent into the coroutine, so the .coro type
23092310
# is wrong.
23102311
assert pdco_outcome is None
2311-
# Explicit "Any" is not allowed
2312-
assert (
2313-
not_none(task).coro.send(
2314-
cast(Any, "be free!"), # type: ignore[misc]
2315-
)
2316-
== "I'm free!"
2317-
)
2312+
# `Argument 1 to "send" of "Coroutine" has incompatible type "str"; expected "Outcome[object]"`
2313+
assert not_none(task).coro.send("be free!") == "I'm free!" # type: ignore[arg-type]
23182314
assert pdco_outcome == outcome.Value("be free!")
23192315
with pytest.raises(StopIteration):
2320-
not_none(task).coro.send(
2321-
cast(Any, None), # type: ignore[misc]
2322-
)
2316+
not_none(task).coro.send(None) # type: ignore[arg-type]
23232317

23242318
# Check the exception paths too
23252319
task = None
@@ -2332,7 +2326,7 @@ async def detachable_coroutine(
23322326
assert not_none(task).coro.throw(throw_in) == "uh oh"
23332327
assert pdco_outcome == outcome.Error(throw_in)
23342328
with pytest.raises(StopIteration):
2335-
task.coro.send(cast(Any, None))
2329+
task.coro.send(None)
23362330

23372331
async def bad_detach() -> None:
23382332
async with _core.open_nursery():
@@ -2384,25 +2378,10 @@ def abort_fn(_: _core.RaiseCancelT) -> _core.Abort: # pragma: no cover
23842378
await wait_all_tasks_blocked()
23852379

23862380
# Okay, it's detached. Here's our coroutine runner:
2387-
# Explicit "Any" is not allowed
2388-
assert (
2389-
not_none(task).coro.send(
2390-
cast(Any, "not trio!"), # type: ignore[misc]
2391-
)
2392-
== 1
2393-
)
2394-
assert (
2395-
not_none(task).coro.send(
2396-
cast(Any, None), # type: ignore[misc]
2397-
)
2398-
== 2
2399-
)
2400-
assert (
2401-
not_none(task).coro.send(
2402-
cast(Any, None), # type: ignore[misc]
2403-
)
2404-
== "byebye"
2405-
)
2381+
# `Argument 1 to "send" of "Coroutine" has incompatible type "str"; expected "Outcome[object]"`
2382+
assert not_none(task).coro.send("not trio!") == 1 # type: ignore[arg-type]
2383+
assert not_none(task).coro.send(None) == 2 # type: ignore[arg-type]
2384+
assert not_none(task).coro.send(None) == "byebye" # type: ignore[arg-type]
24062385

24072386
# Now it's been reattached, and we can leave the nursery
24082387

@@ -2432,9 +2411,8 @@ def abort_fn(_: _core.RaiseCancelT) -> _core.Abort:
24322411
await wait_all_tasks_blocked()
24332412
assert task is not None
24342413
nursery.cancel_scope.cancel()
2435-
task.coro.send(
2436-
cast(Any, None), # type: ignore[misc]
2437-
)
2414+
# `Argument 1 to "send" of "Coroutine" has incompatible type "None"; expected "Outcome[object]"`
2415+
task.coro.send(None) # type: ignore[arg-type]
24382416

24392417
assert abort_fn_called
24402418

src/trio/_core/_traps.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ def abort(inner_raise_cancel):
210210
return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
211211

212212

213-
# Explicit "Any" is not allowed
214-
async def permanently_detach_coroutine_object( # type: ignore[misc]
213+
async def permanently_detach_coroutine_object(
215214
final_outcome: outcome.Outcome[object],
216-
) -> Any:
215+
) -> object:
217216
"""Permanently detach the current task from the Trio scheduler.
218217
219218
Normally, a Trio task doesn't exit until its coroutine object exits. When

src/trio/_socket.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,7 @@ async def sendto(
750750
__address: tuple[object, ...] | str | Buffer,
751751
) -> int: ...
752752

753-
# Explicit "Any" is not allowed
754-
async def sendto(self, *args: Any) -> int: # type: ignore[misc]
753+
async def sendto(self, *args: object) -> int:
755754
raise NotImplementedError
756755

757756
if sys.platform != "win32" or (

0 commit comments

Comments
 (0)