Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into enable-flake8-annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
CoolCat467 committed Nov 4, 2024
2 parents 1d7fd94 + c7801ae commit cf7df04
Show file tree
Hide file tree
Showing 38 changed files with 416 additions and 224 deletions.
1 change: 1 addition & 0 deletions newsfragments/3121.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type annotations in several places by removing `Any` usage.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ warn_return_any = true

# Avoid subtle backsliding
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_generics = true
disallow_any_unimported = true
disallow_incomplete_defs = true
Expand Down
9 changes: 5 additions & 4 deletions src/trio/_core/_concat_tb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from types import TracebackType
from typing import Any, ClassVar, cast
from typing import TYPE_CHECKING, ClassVar, cast

################################################################
# concat_tb
Expand Down Expand Up @@ -88,7 +88,7 @@ def copy_tb(base_tb: TracebackType, tb_next: TracebackType | None) -> TracebackT
# cpython/pypy in current type checkers.
def controller( # type: ignore[no-any-unimported]
operation: tputil.ProxyOperation,
) -> Any | None:
) -> TracebackType | None:
# Rationale for pragma: I looked fairly carefully and tried a few
# things, and AFAICT it's not actually possible to get any
# 'opname' that isn't __getattr__ or __getattribute__. So there's
Expand All @@ -101,9 +101,10 @@ def controller( # type: ignore[no-any-unimported]
"__getattr__",
}
and operation.args[0] == "tb_next"
): # pragma: no cover
) or TYPE_CHECKING: # pragma: no cover
return tb_next
return operation.delegate() # Delegate is reverting to original behaviour
# Delegate is reverting to original behaviour
return operation.delegate() # type: ignore[no-any-return]

return cast(
TracebackType,
Expand Down
3 changes: 2 additions & 1 deletion src/trio/_core/_entry_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

PosArgsT = TypeVarTuple("PosArgsT")

Function = Callable[..., object]
# Explicit "Any" is not allowed
Function = Callable[..., object] # type: ignore[misc]
Job = tuple[Function, tuple[object, ...]]


Expand Down
10 changes: 6 additions & 4 deletions src/trio/_core/_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import logging
import types
from collections.abc import Callable, Sequence
from typing import Any, TypeVar
from typing import TypeVar

from .._abc import Instrument

# Used to log exceptions in instruments
INSTRUMENT_LOGGER = logging.getLogger("trio.abc.Instrument")


F = TypeVar("F", bound=Callable[..., Any])
# Explicit "Any" is not allowed
F = TypeVar("F", bound=Callable[..., object]) # type: ignore[misc]


# Decorator to mark methods public. This does nothing by itself, but
# trio/_tools/gen_exports.py looks for it.
def _public(fn: F) -> F:
# Explicit "Any" is not allowed
def _public(fn: F) -> F: # type: ignore[misc]
return fn


Expand Down Expand Up @@ -92,7 +94,7 @@ def remove_instrument(self, instrument: Instrument) -> None:
def call(
self,
hookname: str,
*args: Any,
*args: object,
) -> None:
"""Call hookname(*args) on each applicable instrument.
Expand Down
26 changes: 21 additions & 5 deletions src/trio/_core/_io_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from contextlib import contextmanager
from typing import (
TYPE_CHECKING,
Any,
Literal,
Protocol,
TypeVar,
cast,
)
Expand All @@ -24,6 +24,7 @@
AFDPollFlags,
CData,
CompletionModes,
CType,
ErrorCodes,
FileFlags,
Handle,
Expand Down Expand Up @@ -249,13 +250,28 @@ class AFDWaiters:
current_op: AFDPollOp | None = None


# Just used for internal type checking.
class _AFDHandle(Protocol):
Handle: Handle
Status: int
Events: int


# Just used for internal type checking.
class _AFDPollInfo(Protocol):
Timeout: int
NumberOfHandles: int
Exclusive: int
Handles: list[_AFDHandle]


# We also need to bundle up all the info for a single op into a standalone
# object, because we need to keep all these objects alive until the operation
# finishes, even if we're throwing it away.
@attrs.frozen(eq=False)
class AFDPollOp:
lpOverlapped: CData
poll_info: Any
poll_info: _AFDPollInfo
waiters: AFDWaiters
afd_group: AFDGroup

Expand Down Expand Up @@ -684,7 +700,7 @@ def _refresh_afd(self, base_handle: Handle) -> None:

lpOverlapped = ffi.new("LPOVERLAPPED")

poll_info: Any = ffi.new("AFD_POLL_INFO *")
poll_info = cast(_AFDPollInfo, ffi.new("AFD_POLL_INFO *"))
poll_info.Timeout = 2**63 - 1 # INT64_MAX
poll_info.NumberOfHandles = 1
poll_info.Exclusive = 0
Expand All @@ -697,9 +713,9 @@ def _refresh_afd(self, base_handle: Handle) -> None:
kernel32.DeviceIoControl(
afd_group.handle,
IoControlCodes.IOCTL_AFD_POLL,
poll_info,
cast(CType, poll_info),
ffi.sizeof("AFD_POLL_INFO"),
poll_info,
cast(CType, poll_info),
ffi.sizeof("AFD_POLL_INFO"),
ffi.NULL,
lpOverlapped,
Expand Down
9 changes: 7 additions & 2 deletions src/trio/_core/_ki.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import types
import weakref
from typing import TYPE_CHECKING, Any, Generic, Protocol, TypeVar
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar

import attrs

Expand Down Expand Up @@ -85,7 +85,12 @@ class _IdRef(weakref.ref[_T]):
__slots__ = ("_hash",)
_hash: int

def __new__(cls, ob: _T, callback: Callable[[Self], Any] | None = None, /) -> Self:
def __new__(
cls,
ob: _T,
callback: Callable[[Self], object] | None = None,
/,
) -> Self:
self: Self = weakref.ref.__new__(cls, ob, callback)
self._hash = object.__hash__(ob)
return self
Expand Down
Loading

0 comments on commit cf7df04

Please sign in to comment.