Skip to content

Commit ad9462b

Browse files
authored
Merge pull request #3201 from tjstum/resolver
Update to mypy 1.15
2 parents 26ea897 + 3d61e02 commit ad9462b

28 files changed

Lines changed: 110 additions & 128 deletions

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ repos:
3434
rev: v2.4.1
3535
hooks:
3636
- id: codespell
37+
additional_dependencies:
38+
# tomli needed on 3.10. tomllib is available in stdlib on 3.11+
39+
- tomli
3740
- repo: https://github.com/crate-ci/typos
3841
rev: typos-dict-v0.12.4
3942
hooks:

newsfragments/3201.misc.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The typing of :func:`trio.abc.HostnameResolver.getaddrinfo` has been corrected to
2+
match that of the stdlib `socket.getaddrinfo`, which was updated in mypy 1.15 (via
3+
a typeshed update) to include the possibility of ``tuple[int, bytes]`` for the
4+
``sockaddr`` field of the result. This happens in situations where Python was compiled
5+
with ``--disable-ipv6``.
6+
7+
Additionally, the static typing of :func:`trio.to_thread.run_sync`,
8+
:func:`trio.from_thread.run` and :func:`trio.from_thread.run_sync` has been
9+
improved and should reflect the underlying function being run.

src/trio/_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def getaddrinfo(
178178
socket.SocketKind,
179179
int,
180180
str,
181-
tuple[str, int] | tuple[str, int, int, int],
181+
tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes],
182182
]
183183
]:
184184
"""A custom implementation of :func:`~trio.socket.getaddrinfo`.

src/trio/_core/_entry_queue.py

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

1717
PosArgsT = TypeVarTuple("PosArgsT")
1818

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

2322

src/trio/_core/_instrumentation.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
import logging
44
import types
5-
from collections.abc import Callable, Sequence
6-
from typing import TypeVar
5+
from typing import TYPE_CHECKING, TypeVar
76

87
from .._abc import Instrument
98

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

12+
if TYPE_CHECKING:
13+
from collections.abc import Sequence
1314

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

1717

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

2423

src/trio/_core/_run.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
from collections.abc import (
6262
Awaitable,
6363
Callable,
64-
Coroutine,
6564
Generator,
6665
Iterator,
6766
Sequence,
@@ -1303,8 +1302,7 @@ def start_soon(
13031302
GLOBAL_RUN_CONTEXT.runner.spawn_impl(async_fn, args, self, name)
13041303

13051304
# Typing changes blocked by https://github.com/python/mypy/pull/17512
1306-
# Explicit "Any" is not allowed
1307-
async def start( # type: ignore[misc]
1305+
async def start( # type: ignore[explicit-any]
13081306
self,
13091307
async_fn: Callable[..., Awaitable[object]],
13101308
*args: object,
@@ -1404,10 +1402,9 @@ def __del__(self) -> None:
14041402

14051403
@final
14061404
@attrs.define(eq=False, repr=False)
1407-
class Task(metaclass=NoPublicConstructor): # type: ignore[misc]
1405+
class Task(metaclass=NoPublicConstructor): # type: ignore[explicit-any]
14081406
_parent_nursery: Nursery | None
1409-
# Explicit "Any" is not allowed
1410-
coro: Coroutine[Any, Outcome[object], Any] # type: ignore[misc]
1407+
coro: types.CoroutineType[Any, Outcome[object], Any] # type: ignore[explicit-any]
14111408
_runner: Runner
14121409
name: str
14131410
context: contextvars.Context
@@ -1425,11 +1422,10 @@ class Task(metaclass=NoPublicConstructor): # type: ignore[misc]
14251422
# tracebacks with extraneous frames.
14261423
# - for scheduled tasks, custom_sleep_data is None
14271424
# Tasks start out unscheduled.
1428-
# Explicit "Any" is not allowed
1429-
_next_send_fn: Callable[[Any], object] | None = None # type: ignore[misc]
1430-
_next_send: Outcome[Any] | BaseException | None = None # type: ignore[misc]
1425+
_next_send_fn: Callable[[Any], object] | None = None # type: ignore[explicit-any]
1426+
_next_send: Outcome[Any] | BaseException | None = None # type: ignore[explicit-any]
14311427
_abort_func: Callable[[_core.RaiseCancelT], Abort] | None = None
1432-
custom_sleep_data: Any = None # type: ignore[misc]
1428+
custom_sleep_data: Any = None # type: ignore[explicit-any]
14331429

14341430
# For introspection and nursery.start()
14351431
_child_nurseries: list[Nursery] = attrs.Factory(list)
@@ -1497,7 +1493,7 @@ def print_stack_for_task(task):
14971493
14981494
"""
14991495
# Ignore static typing as we're doing lots of dynamic introspection
1500-
coro: Any = self.coro # type: ignore[misc]
1496+
coro: Any = self.coro # type: ignore[explicit-any]
15011497
while coro is not None:
15021498
if hasattr(coro, "cr_frame"):
15031499
# A real coroutine
@@ -1642,16 +1638,13 @@ class RunStatistics:
16421638

16431639

16441640
@attrs.define(eq=False)
1645-
# Explicit "Any" is not allowed
1646-
class GuestState: # type: ignore[misc]
1641+
class GuestState: # type: ignore[explicit-any]
16471642
runner: Runner
16481643
run_sync_soon_threadsafe: Callable[[Callable[[], object]], object]
16491644
run_sync_soon_not_threadsafe: Callable[[Callable[[], object]], object]
1650-
# Explicit "Any" is not allowed
1651-
done_callback: Callable[[Outcome[Any]], object] # type: ignore[misc]
1645+
done_callback: Callable[[Outcome[Any]], object] # type: ignore[explicit-any]
16521646
unrolled_run_gen: Generator[float, EventResult, None]
1653-
# Explicit "Any" is not allowed
1654-
unrolled_run_next_send: Outcome[Any] = attrs.Factory(lambda: Value(None)) # type: ignore[misc]
1647+
unrolled_run_next_send: Outcome[Any] = attrs.Factory(lambda: Value(None)) # type: ignore[explicit-any]
16551648

16561649
def guest_tick(self) -> None:
16571650
prev_library, sniffio_library.name = sniffio_library.name, "trio"
@@ -1696,17 +1689,15 @@ def in_main_thread() -> None:
16961689

16971690

16981691
@attrs.define(eq=False)
1699-
# Explicit "Any" is not allowed
1700-
class Runner: # type: ignore[misc]
1692+
class Runner: # type: ignore[explicit-any]
17011693
clock: Clock
17021694
instruments: Instruments
17031695
io_manager: TheIOManager
17041696
ki_manager: KIManager
17051697
strict_exception_groups: bool
17061698

17071699
# Run-local values, see _local.py
1708-
# Explicit "Any" is not allowed
1709-
_locals: dict[_core.RunVar[Any], object] = attrs.Factory(dict) # type: ignore[misc]
1700+
_locals: dict[_core.RunVar[Any], object] = attrs.Factory(dict) # type: ignore[explicit-any]
17101701

17111702
runq: deque[Task] = attrs.Factory(deque)
17121703
tasks: set[Task] = attrs.Factory(set)
@@ -1895,7 +1886,7 @@ async def python_wrapper(orig_coro: Awaitable[RetT]) -> RetT:
18951886
return await orig_coro
18961887

18971888
coro = python_wrapper(coro)
1898-
assert coro.cr_frame is not None, "Coroutine frame should exist"
1889+
assert coro.cr_frame is not None, "Coroutine frame should exist" # type: ignore[attr-defined]
18991890

19001891
######
19011892
# Set up the Task object
@@ -2133,8 +2124,7 @@ def _deliver_ki_cb(self) -> None:
21332124

21342125
# sortedcontainers doesn't have types, and is reportedly very hard to type:
21352126
# https://github.com/grantjenks/python-sortedcontainers/issues/68
2136-
# Explicit "Any" is not allowed
2137-
waiting_for_idle: Any = attrs.Factory(SortedDict) # type: ignore[misc]
2127+
waiting_for_idle: Any = attrs.Factory(SortedDict) # type: ignore[explicit-any]
21382128

21392129
@_public
21402130
async def wait_all_tasks_blocked(self, cushion: float = 0.0) -> None:
@@ -2435,8 +2425,7 @@ def run(
24352425
raise AssertionError(runner.main_task_outcome)
24362426

24372427

2438-
# Explicit .../"Any" not allowed
2439-
def start_guest_run( # type: ignore[misc]
2428+
def start_guest_run( # type: ignore[explicit-any]
24402429
async_fn: Callable[..., Awaitable[RetT]],
24412430
*args: object,
24422431
run_sync_soon_threadsafe: Callable[[Callable[[], object]], object],

src/trio/_core/_tests/test_asyncgen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import weakref
66
from math import inf
7+
from types import AsyncGeneratorType
78
from typing import TYPE_CHECKING, NoReturn
89

910
import pytest
@@ -88,6 +89,7 @@ async def async_main() -> None:
8889
_core.run(async_main)
8990
assert collected.pop() == "outlived run"
9091
for agen in saved:
92+
assert isinstance(agen, AsyncGeneratorType)
9193
assert agen.ag_frame is None # all should now be exhausted
9294

9395

@@ -301,9 +303,11 @@ def test_delegation_to_existing_hooks() -> None:
301303
record = []
302304

303305
def my_firstiter(agen: AsyncGenerator[object, NoReturn]) -> None:
306+
assert isinstance(agen, AsyncGeneratorType)
304307
record.append("firstiter " + agen.ag_frame.f_locals["arg"])
305308

306309
def my_finalizer(agen: AsyncGenerator[object, NoReturn]) -> None:
310+
assert isinstance(agen, AsyncGeneratorType)
307311
record.append("finalizer " + agen.ag_frame.f_locals["arg"])
308312

309313
async def example(arg: str) -> AsyncGenerator[int, None]:

src/trio/_core/_tests/test_ki.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def protected_manager() -> Iterator[None]:
165165
async def test_async_generator_agen_protection() -> None:
166166
@_core.enable_ki_protection
167167
@async_generator # type: ignore[misc] # untyped generator
168-
async def agen_protected1() -> None:
168+
async def agen_protected1() -> None: # type: ignore[misc] # untyped generator
169169
assert _core.currently_ki_protected()
170170
try:
171171
await yield_()
@@ -174,7 +174,7 @@ async def agen_protected1() -> None:
174174

175175
@_core.disable_ki_protection
176176
@async_generator # type: ignore[misc] # untyped generator
177-
async def agen_unprotected1() -> None:
177+
async def agen_unprotected1() -> None: # type: ignore[misc] # untyped generator
178178
assert not _core.currently_ki_protected()
179179
try:
180180
await yield_()
@@ -184,7 +184,7 @@ async def agen_unprotected1() -> None:
184184
# Swap the order of the decorators:
185185
@async_generator # type: ignore[misc] # untyped generator
186186
@_core.enable_ki_protection
187-
async def agen_protected2() -> None:
187+
async def agen_protected2() -> None: # type: ignore[misc] # untyped generator
188188
assert _core.currently_ki_protected()
189189
try:
190190
await yield_()
@@ -193,7 +193,7 @@ async def agen_protected2() -> None:
193193

194194
@async_generator # type: ignore[misc] # untyped generator
195195
@_core.disable_ki_protection
196-
async def agen_unprotected2() -> None:
196+
async def agen_unprotected2() -> None: # type: ignore[misc] # untyped generator
197197
assert not _core.currently_ki_protected()
198198
try:
199199
await yield_()
@@ -677,9 +677,8 @@ async def _consume_async_generator(agen: AsyncGenerator[None, None]) -> None:
677677
await agen.aclose()
678678

679679

680-
# Explicit .../"Any" is not allowed
681-
def _consume_function_for_coverage( # type: ignore[misc]
682-
fn: Callable[..., object],
680+
def _consume_function_for_coverage(
681+
fn: Callable[[], object],
683682
) -> None:
684683
result = fn()
685684
if inspect.isasyncgen(result):

src/trio/_core/_tests/test_run.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,8 +1658,7 @@ async def func1(expected: str) -> None:
16581658
async def func2() -> None: # pragma: no cover
16591659
pass
16601660

1661-
# Explicit .../"Any" is not allowed
1662-
async def check( # type: ignore[misc]
1661+
async def check( # type: ignore[explicit-any]
16631662
spawn_fn: Callable[..., object],
16641663
) -> None:
16651664
spawn_fn(func1, "func1")
@@ -1696,14 +1695,13 @@ async def test_current_effective_deadline(mock_clock: _core.MockClock) -> None:
16961695

16971696

16981697
def test_nice_error_on_bad_calls_to_run_or_spawn() -> None:
1699-
# Explicit .../"Any" is not allowed
1700-
def bad_call_run( # type: ignore[misc]
1698+
def bad_call_run( # type: ignore[explicit-any]
17011699
func: Callable[..., Awaitable[object]],
17021700
*args: tuple[object, ...],
17031701
) -> None:
17041702
_core.run(func, *args)
17051703

1706-
def bad_call_spawn( # type: ignore[misc]
1704+
def bad_call_spawn( # type: ignore[explicit-any]
17071705
func: Callable[..., Awaitable[object]],
17081706
*args: tuple[object, ...],
17091707
) -> None:

src/trio/_core/_thread_cache.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ class ThreadCache:
215215
__slots__ = ("_idle_workers",)
216216

217217
def __init__(self) -> None:
218-
# Explicit "Any" not allowed
219-
self._idle_workers: dict[WorkerThread[Any], None] = {} # type: ignore[misc]
218+
self._idle_workers: dict[WorkerThread[Any], None] = {} # type: ignore[explicit-any]
220219

221220
def start_thread_soon(
222221
self,

0 commit comments

Comments
 (0)