Skip to content

Commit

Permalink
Merge branch 'main' into avoid-refcycles-in-run-exc
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert authored Nov 21, 2024
2 parents 12e8395 + 7334c42 commit afb6752
Show file tree
Hide file tree
Showing 55 changed files with 690 additions and 433 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python: ['pypy-3.10', '3.9', '3.10', '3.11', '3.12', '3.13']
python: ['3.9', '3.10', '3.11', '3.12', '3.13']
arch: ['x86', 'x64']
lsp: ['']
lsp_extract_file: ['']
Expand All @@ -34,6 +34,11 @@ jobs:
lsp: 'https://www.proxifier.com/download/legacy/ProxifierSetup342.exe'
lsp_extract_file: ''
extra_name: ', with IFS LSP'
- python: 'pypy-3.10'
arch: 'x64'
lsp: ''
lsp_extract_file: ''
extra_name: ''
#- python: '3.9'
# arch: 'x64'
# lsp: 'http://download.pctools.com/mirror/updates/9.0.0.2308-SDavfree-lite_en.exe'
Expand Down
6 changes: 2 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
ci:
autofix_commit_msg: "[pre-commit.ci] auto fixes from pre-commit.com hooks"
autofix_prs: false
autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate"
autofix_prs: true
autoupdate_schedule: weekly
submodules: false
skip: [regenerate-files]
Expand All @@ -24,7 +22,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
rev: v0.7.4
hooks:
- id: ruff
types: [file]
Expand Down
16 changes: 8 additions & 8 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file was autogenerated by uv via the following command:
# uv pip compile --universal --python-version=3.11 docs-requirements.in -o docs-requirements.txt
alabaster==0.7.16
alabaster==1.0.0
# via sphinx
attrs==24.2.0
# via
Expand All @@ -16,17 +16,17 @@ cffi==1.17.1 ; platform_python_implementation != 'PyPy' or os_name == 'nt'
# via
# -r docs-requirements.in
# cryptography
charset-normalizer==3.3.2
charset-normalizer==3.4.0
# via requests
click==8.1.7
# via towncrier
colorama==0.4.6 ; sys_platform == 'win32' or platform_system == 'Windows'
# via
# click
# sphinx
cryptography==43.0.1
cryptography==43.0.3
# via pyopenssl
docutils==0.20.1
docutils==0.21.2
# via
# sphinx
# sphinx-rtd-theme
Expand All @@ -38,14 +38,14 @@ idna==3.10
# requests
imagesize==1.4.1
# via sphinx
immutables==0.20
immutables==0.21
# via -r docs-requirements.in
jinja2==3.1.4
# via
# -r docs-requirements.in
# sphinx
# towncrier
markupsafe==2.1.5
markupsafe==3.0.2
# via jinja2
outcome==1.3.0.post0
# via -r docs-requirements.in
Expand All @@ -67,7 +67,7 @@ sortedcontainers==2.4.0
# via -r docs-requirements.in
soupsieve==2.6
# via beautifulsoup4
sphinx==7.4.7
sphinx==8.1.3
# via
# -r docs-requirements.in
# sphinx-codeautolink
Expand All @@ -79,7 +79,7 @@ sphinx-codeautolink==0.15.2
# via -r docs-requirements.in
sphinx-hoverxref==1.4.1
# via -r docs-requirements.in
sphinx-rtd-theme==3.0.0
sphinx-rtd-theme==3.0.1
# via -r docs-requirements.in
sphinxcontrib-applehelp==2.0.0
# via sphinx
Expand Down
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.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ allowed-confusables = ["–"]

select = [
"A", # flake8-builtins
"ANN", # flake8-annotations
"ASYNC", # flake8-async
"B", # flake8-bugbear
"C4", # flake8-comprehensions
Expand All @@ -131,6 +132,9 @@ select = [
]
extend-ignore = [
'A002', # builtin-argument-shadowing
'ANN101', # missing-type-self
'ANN102', # missing-type-cls
'ANN401', # any-type (mypy's `disallow_any_explicit` is better)
'E402', # module-import-not-at-top-of-file (usually OS-specific)
'E501', # line-too-long
'F403', # undefined-local-with-import-star
Expand Down Expand Up @@ -160,6 +164,8 @@ extend-ignore = [
'src/trio/_abc.py' = ['A005']
'src/trio/_socket.py' = ['A005']
'src/trio/_ssl.py' = ['A005']
# Don't check annotations in notes-to-self
'notes-to-self/*.py' = ['ANN001', 'ANN002', 'ANN003', 'ANN201', 'ANN202', 'ANN204']

[tool.ruff.lint.isort]
combine-as-imports = true
Expand All @@ -183,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
2 changes: 1 addition & 1 deletion src/trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __new__( # type: ignore[misc] # "must return a subtype"
) -> tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]:
return _open_memory_channel(max_buffer_size)

def __init__(self, max_buffer_size: int | float): # noqa: PYI041
def __init__(self, max_buffer_size: int | float) -> None: # noqa: PYI041
...

else:
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) -> TracebackT
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]
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 @@ def controller(operation: tputil.ProxyOperation) -> Any | None: # type: ignore[
"__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
16 changes: 11 additions & 5 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 All @@ -32,7 +34,7 @@ class Instruments(dict[str, dict[Instrument, None]]):

__slots__ = ()

def __init__(self, incoming: Sequence[Instrument]):
def __init__(self, incoming: Sequence[Instrument]) -> None:
self["_all"] = {}
for instrument in incoming:
self.add_instrument(instrument)
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 @@ 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
2 changes: 1 addition & 1 deletion src/trio/_core/_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MockClock(Clock):
"""

def __init__(self, rate: float = 0.0, autojump_threshold: float = inf):
def __init__(self, rate: float = 0.0, autojump_threshold: float = inf) -> None:
# when the real clock said 'real_base', the virtual time was
# 'virtual_base', and since then it's advanced at 'rate' virtual
# seconds per real second.
Expand Down
Loading

0 comments on commit afb6752

Please sign in to comment.