Skip to content

Enable mypy's disallow_any_explicit flag. #3121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6e91b75
Enable mypy's `disallow_any_explicit` flag.
CoolCat467 Oct 27, 2024
feeccf1
Fix various things after reviewing all changes
CoolCat467 Oct 27, 2024
569bc37
Do not use faster mypy cache on pypy
CoolCat467 Oct 27, 2024
deb1c13
Merge remote-tracking branch 'origin/main' into enable-disallow_any_e…
CoolCat467 Oct 27, 2024
521c1b7
Try to get rid of more `Any`s
CoolCat467 Oct 27, 2024
55964ad
Get rid of a bunch more `Any`s and resolve mypy issues
CoolCat467 Oct 28, 2024
e888514
Match other TypeVarTuple usage
CoolCat467 Oct 28, 2024
30db0d8
Mark variable as nonlocal
CoolCat467 Oct 28, 2024
7707361
Cast asyncio run in loop to be InHost
CoolCat467 Oct 28, 2024
e99b69f
Handle case where `names` does not exist in node for some reason
CoolCat467 Oct 28, 2024
aee4a77
Hopefully fix jedi issue
CoolCat467 Oct 28, 2024
e157589
Check the hashes are the same
A5rocks Oct 28, 2024
62f89fa
Get the hash glob makes
A5rocks Oct 28, 2024
ca48ef6
Fix inputs to glob.hashFiles
A5rocks Oct 28, 2024
28b4abc
Remove testing hashes
A5rocks Oct 28, 2024
a1e4916
Apply requested changes from code review
CoolCat467 Oct 28, 2024
c3c0a28
Code review suggestions
CoolCat467 Oct 28, 2024
63353f4
Fix type issue
CoolCat467 Oct 28, 2024
69e60a5
Fix cffi type issues again
CoolCat467 Oct 28, 2024
25ecf1e
Merge branch 'main' into enable-disallow_any_explicit
CoolCat467 Oct 28, 2024
2bc0da3
Use correct `CData`
CoolCat467 Oct 28, 2024
0f999b8
Fix cast again
CoolCat467 Oct 28, 2024
50adccb
Clarify comments and get rid of more `Any`s
CoolCat467 Oct 30, 2024
3a320c3
Update src/trio/_tests/test_socket.py
CoolCat467 Oct 30, 2024
03f1c4e
Apply suggestions from code review
CoolCat467 Oct 30, 2024
33f1caa
Apply suggestions from code review
CoolCat467 Oct 30, 2024
854c5cd
Update src/trio/_threads.py
CoolCat467 Oct 30, 2024
bcd3f54
Improve type annotations
CoolCat467 Oct 30, 2024
7e719a8
Merge branch 'main' into enable-disallow_any_explicit
CoolCat467 Oct 30, 2024
ee0322a
Forgot `type` and fix more type issues
CoolCat467 Oct 30, 2024
9005f98
Add typing to `orig_getaddrinfo`
CoolCat467 Oct 30, 2024
c480449
Add full typing for `_responses` and `record`
CoolCat467 Oct 30, 2024
ba24f74
Fix type issues
CoolCat467 Oct 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -183,6 +183,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
11 changes: 7 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 @@ -86,7 +86,9 @@
def copy_tb(base_tb: TracebackType, tb_next: TracebackType | None) -> TracebackType:
# tputil.ProxyOperation is PyPy-only, and there's no way to specify
# cpython/pypy in current type checkers.
def controller(operation: tputil.ProxyOperation) -> Any | None: # type: ignore[no-any-unimported]
def controller( # type: ignore[no-any-unimported]

Check warning on line 89 in src/trio/_core/_concat_tb.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_core/_concat_tb.py#L89

Added line #L89 was not covered by tests
operation: tputil.ProxyOperation,
) -> 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 @@ -99,9 +101,10 @@
"__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]

Check warning on line 107 in src/trio/_core/_concat_tb.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_core/_concat_tb.py#L107

Added line #L107 was not covered by tests

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
14 changes: 10 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 @@ -89,7 +91,11 @@ def remove_instrument(self, instrument: Instrument) -> None:
if not instruments:
del self[hookname]

def call(self, hookname: str, *args: Any) -> None:
def call(
self,
hookname: str,
*args: object,
) -> None:
"""Call hookname(*args) on each applicable instrument.

You must first check whether there are any instruments installed for
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 @@
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

Check warning on line 274 in src/trio/_core/_io_windows.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_core/_io_windows.py#L274

Added line #L274 was not covered by tests
waiters: AFDWaiters
afd_group: AFDGroup

Expand Down Expand Up @@ -684,7 +700,7 @@

lpOverlapped = ffi.new("LPOVERLAPPED")

poll_info: Any = ffi.new("AFD_POLL_INFO *")
poll_info = cast(_AFDPollInfo, ffi.new("AFD_POLL_INFO *"))

Check warning on line 703 in src/trio/_core/_io_windows.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_core/_io_windows.py#L703

Added line #L703 was not covered by tests
poll_info.Timeout = 2**63 - 1 # INT64_MAX
poll_info.NumberOfHandles = 1
poll_info.Exclusive = 0
Expand All @@ -697,9 +713,9 @@
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
Loading