Skip to content

Commit c7801ae

Browse files
authored
Merge pull request #3121 from CoolCat467/enable-disallow_any_explicit
Enable mypy's `disallow_any_explicit` flag.
2 parents 9d6e134 + ba24f74 commit c7801ae

38 files changed

+503
-227
lines changed

newsfragments/3121.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type annotations in several places by removing `Any` usage.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ warn_return_any = true
183183

184184
# Avoid subtle backsliding
185185
disallow_any_decorated = true
186+
disallow_any_explicit = true
186187
disallow_any_generics = true
187188
disallow_any_unimported = true
188189
disallow_incomplete_defs = true

src/trio/_core/_concat_tb.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from types import TracebackType
4-
from typing import Any, ClassVar, cast
4+
from typing import TYPE_CHECKING, ClassVar, cast
55

66
################################################################
77
# concat_tb
@@ -86,7 +86,9 @@ def copy_tb(base_tb: TracebackType, tb_next: TracebackType | None) -> TracebackT
8686
def copy_tb(base_tb: TracebackType, tb_next: TracebackType | None) -> TracebackType:
8787
# tputil.ProxyOperation is PyPy-only, and there's no way to specify
8888
# cpython/pypy in current type checkers.
89-
def controller(operation: tputil.ProxyOperation) -> Any | None: # type: ignore[no-any-unimported]
89+
def controller( # type: ignore[no-any-unimported]
90+
operation: tputil.ProxyOperation,
91+
) -> TracebackType | None:
9092
# Rationale for pragma: I looked fairly carefully and tried a few
9193
# things, and AFAICT it's not actually possible to get any
9294
# 'opname' that isn't __getattr__ or __getattribute__. So there's
@@ -99,9 +101,10 @@ def controller(operation: tputil.ProxyOperation) -> Any | None: # type: ignore[
99101
"__getattr__",
100102
}
101103
and operation.args[0] == "tb_next"
102-
): # pragma: no cover
104+
) or TYPE_CHECKING: # pragma: no cover
103105
return tb_next
104-
return operation.delegate() # Delegate is reverting to original behaviour
106+
# Delegate is reverting to original behaviour
107+
return operation.delegate() # type: ignore[no-any-return]
105108

106109
return cast(
107110
TracebackType,

src/trio/_core/_entry_queue.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
PosArgsT = TypeVarTuple("PosArgsT")
1818

19-
Function = Callable[..., object]
19+
# Explicit "Any" is not allowed
20+
Function = Callable[..., object] # type: ignore[misc]
2021
Job = tuple[Function, tuple[object, ...]]
2122

2223

src/trio/_core/_instrumentation.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
import logging
44
import types
55
from collections.abc import Callable, Sequence
6-
from typing import Any, TypeVar
6+
from typing import TypeVar
77

88
from .._abc import Instrument
99

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

1313

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

1617

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

2224

@@ -89,7 +91,11 @@ def remove_instrument(self, instrument: Instrument) -> None:
8991
if not instruments:
9092
del self[hookname]
9193

92-
def call(self, hookname: str, *args: Any) -> None:
94+
def call(
95+
self,
96+
hookname: str,
97+
*args: object,
98+
) -> None:
9399
"""Call hookname(*args) on each applicable instrument.
94100
95101
You must first check whether there are any instruments installed for

src/trio/_core/_io_windows.py

Lines changed: 21 additions & 5 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
)
@@ -24,6 +24,7 @@
2424
AFDPollFlags,
2525
CData,
2626
CompletionModes,
27+
CType,
2728
ErrorCodes,
2829
FileFlags,
2930
Handle,
@@ -249,13 +250,28 @@ class AFDWaiters:
249250
current_op: AFDPollOp | None = None
250251

251252

253+
# Just used for internal type checking.
254+
class _AFDHandle(Protocol):
255+
Handle: Handle
256+
Status: int
257+
Events: int
258+
259+
260+
# Just used for internal type checking.
261+
class _AFDPollInfo(Protocol):
262+
Timeout: int
263+
NumberOfHandles: int
264+
Exclusive: int
265+
Handles: list[_AFDHandle]
266+
267+
252268
# We also need to bundle up all the info for a single op into a standalone
253269
# object, because we need to keep all these objects alive until the operation
254270
# finishes, even if we're throwing it away.
255271
@attrs.frozen(eq=False)
256272
class AFDPollOp:
257273
lpOverlapped: CData
258-
poll_info: Any
274+
poll_info: _AFDPollInfo
259275
waiters: AFDWaiters
260276
afd_group: AFDGroup
261277

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

685701
lpOverlapped = ffi.new("LPOVERLAPPED")
686702

687-
poll_info: Any = ffi.new("AFD_POLL_INFO *")
703+
poll_info = cast(_AFDPollInfo, ffi.new("AFD_POLL_INFO *"))
688704
poll_info.Timeout = 2**63 - 1 # INT64_MAX
689705
poll_info.NumberOfHandles = 1
690706
poll_info.Exclusive = 0
@@ -697,9 +713,9 @@ def _refresh_afd(self, base_handle: Handle) -> None:
697713
kernel32.DeviceIoControl(
698714
afd_group.handle,
699715
IoControlCodes.IOCTL_AFD_POLL,
700-
poll_info,
716+
cast(CType, poll_info),
701717
ffi.sizeof("AFD_POLL_INFO"),
702-
poll_info,
718+
cast(CType, poll_info),
703719
ffi.sizeof("AFD_POLL_INFO"),
704720
ffi.NULL,
705721
lpOverlapped,

src/trio/_core/_ki.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import types
66
import weakref
7-
from typing import TYPE_CHECKING, Any, Generic, Protocol, TypeVar
7+
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar
88

99
import attrs
1010

@@ -85,7 +85,12 @@ class _IdRef(weakref.ref[_T]):
8585
__slots__ = ("_hash",)
8686
_hash: int
8787

88-
def __new__(cls, ob: _T, callback: Callable[[Self], Any] | None = None, /) -> Self:
88+
def __new__(
89+
cls,
90+
ob: _T,
91+
callback: Callable[[Self], object] | None = None,
92+
/,
93+
) -> Self:
8994
self: Self = weakref.ref.__new__(cls, ob, callback)
9095
self._hash = object.__hash__(ob)
9196
return self

0 commit comments

Comments
 (0)