Skip to content

Commit bcd3f54

Browse files
CoolCat467jakkdl
andcommitted
Improve type annotations
Co-authored-by: John Litborn <[email protected]>
1 parent 854c5cd commit bcd3f54

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

src/trio/_tests/test_highlevel_ssl_helpers.py

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

33
from functools import partial
4-
from typing import TYPE_CHECKING, Any, NoReturn, cast
4+
from typing import TYPE_CHECKING, NoReturn, cast
55

66
import attrs
77
import pytest
@@ -65,11 +65,11 @@ async def getaddrinfo(
6565
]:
6666
return [(AF_INET, SOCK_STREAM, IPPROTO_TCP, "", self.sockaddr)]
6767

68-
# Explicit "Any" is not allowed
69-
async def getnameinfo( # type: ignore[misc]
68+
async def getnameinfo(
7069
self,
71-
*args: Any,
72-
) -> NoReturn: # pragma: no cover
70+
sockaddr: tuple[str, int] | tuple[str, int, int, int],
71+
flags: int,
72+
) -> NoReturn:
7373
raise NotImplementedError
7474

7575

src/trio/_tests/test_socket.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import tempfile
99
from pathlib import Path
1010
from socket import AddressFamily, SocketKind
11-
from typing import TYPE_CHECKING, Any, Callable, Union
11+
from typing import TYPE_CHECKING, Callable, Union, cast
1212

1313
import attrs
1414
import pytest
1515

1616
from .. import _core, socket as tsocket
1717
from .._core._tests.tutil import binds_ipv6, creates_ipv6
18-
from .._socket import _NUMERIC_ONLY, SocketType, _SocketType, _try_sync
18+
from .._socket import _NUMERIC_ONLY, AddressFormat, SocketType, _SocketType, _try_sync
1919
from ..testing import assert_checkpoints, wait_all_tasks_blocked
2020

2121
if TYPE_CHECKING:
@@ -41,50 +41,64 @@
4141

4242

4343
class MonkeypatchedGAI:
44+
__slots__ = ("_orig_getaddrinfo", "_responses", "record")
45+
4446
# Explicit .../"Any" is not allowed
4547
def __init__( # type: ignore[misc]
4648
self,
4749
orig_getaddrinfo: Callable[..., GetAddrInfoResponse],
4850
) -> None:
4951
self._orig_getaddrinfo = orig_getaddrinfo
50-
self._responses: dict[tuple[Any, ...], GetAddrInfoResponse | str] = {} # type: ignore[misc]
51-
self.record: list[tuple[Any, ...]] = [] # type: ignore[misc]
52+
self._responses: dict[
53+
tuple[str | int | bytes | None, ...],
54+
GetAddrInfoResponse | str,
55+
] = {}
56+
self.record: list[tuple[str | int | bytes | None, ...]] = []
5257

5358
# get a normalized getaddrinfo argument tuple
54-
# Explicit "Any" is not allowed
55-
def _frozenbind( # type: ignore[misc]
59+
def _frozenbind(
5660
self,
57-
*args: Any,
58-
**kwargs: Any,
59-
) -> tuple[Any, ...]:
61+
host: str | bytes | None,
62+
port: str | bytes | int | None,
63+
family: int = 0,
64+
type: int = 0,
65+
proto: int = 0,
66+
flags: int = 0,
67+
) -> tuple[str | int | bytes | None, ...]:
6068
sig = inspect.signature(self._orig_getaddrinfo)
61-
bound = sig.bind(*args, **kwargs)
69+
bound = sig.bind(host, port, family, proto, flags)
6270
bound.apply_defaults()
6371
frozenbound = bound.args
6472
assert not bound.kwargs
6573
return frozenbound
6674

67-
# Explicit "Any" is not allowed
68-
def set( # type: ignore[misc]
75+
def set(
6976
self,
7077
response: GetAddrInfoResponse | str,
71-
*args: Any,
72-
**kwargs: Any,
78+
host: str | bytes | None,
79+
port: str | bytes | int | None,
80+
family: int = 0,
81+
type: int = 0,
82+
proto: int = 0,
83+
flags: int = 0,
7384
) -> None:
74-
self._responses[self._frozenbind(*args, **kwargs)] = response
85+
self._responses[self._frozenbind(host, port, family, proto, flags)] = response
7586

76-
# Explicit "Any" is not allowed
77-
def getaddrinfo( # type: ignore[misc]
87+
def getaddrinfo(
7888
self,
79-
*args: Any,
80-
**kwargs: Any,
89+
host: str | bytes | None,
90+
port: str | bytes | int | None,
91+
family: int = 0,
92+
type: int = 0,
93+
proto: int = 0,
94+
flags: int = 0,
8195
) -> GetAddrInfoResponse | str:
82-
bound = self._frozenbind(*args, **kwargs)
96+
bound = self._frozenbind(host, port, family, proto, flags)
8397
self.record.append(bound)
8498
if bound in self._responses:
8599
return self._responses[bound]
86-
elif bound[-1] & stdlib_socket.AI_NUMERICHOST:
87-
return self._orig_getaddrinfo(*args, **kwargs)
100+
elif flags & stdlib_socket.AI_NUMERICHOST:
101+
return self._orig_getaddrinfo(host, port, family, proto, flags)
88102
else:
89103
raise RuntimeError(f"gai called with unexpected arguments {bound}")
90104

@@ -601,8 +615,7 @@ def assert_eq(
601615
# local=True/local=False should work the same:
602616
for local in [False, True]:
603617

604-
# Explicit "Any" is not allowed
605-
async def res( # type: ignore[misc]
618+
async def res(
606619
args: (
607620
tuple[str, int]
608621
| tuple[str, int, int]
@@ -611,11 +624,13 @@ async def res( # type: ignore[misc]
611624
| tuple[str, str, int]
612625
| tuple[str, str, int, int]
613626
),
614-
) -> Any:
615-
return await sock._resolve_address_nocp(
627+
) -> tuple[str | int, ...]:
628+
value = await sock._resolve_address_nocp(
616629
args,
617630
local=local, # noqa: B023 # local is not bound in function definition
618631
)
632+
assert isinstance(value, tuple)
633+
return cast(tuple[Union[str, int], ...], value)
619634

620635
assert_eq(await res((addrs.arbitrary, "http")), (addrs.arbitrary, 80))
621636
if v6:
@@ -810,11 +825,9 @@ async def test_SocketType_connect_paths() -> None:
810825
# nose -- and then swap it back out again before we hit
811826
# wait_socket_writable, which insists on a real socket.
812827
class CancelSocket(stdlib_socket.socket):
813-
# Explicit "Any" is not allowed
814-
def connect( # type: ignore[misc]
828+
def connect(
815829
self,
816-
*args: Any,
817-
**kwargs: Any,
830+
address: AddressFormat,
818831
) -> None:
819832
# accessing private method only available in _SocketType
820833
assert isinstance(sock, _SocketType)
@@ -825,7 +838,7 @@ def connect( # type: ignore[misc]
825838
self.family,
826839
self.type,
827840
)
828-
sock._sock.connect(*args, **kwargs)
841+
sock._sock.connect(address)
829842
# If connect *doesn't* raise, then pretend it did
830843
raise BlockingIOError # pragma: no cover
831844

@@ -871,11 +884,14 @@ async def test_resolve_address_exception_in_connect_closes_socket() -> None:
871884
with _core.CancelScope() as cancel_scope:
872885
with tsocket.socket() as sock:
873886

874-
# Explicit "Any" is not allowed
875-
async def _resolve_address_nocp( # type: ignore[misc]
887+
async def _resolve_address_nocp(
876888
self: _SocketType,
877-
*args: Any,
878-
**kwargs: Any,
889+
host: str | bytes | None,
890+
port: str | bytes | int | None,
891+
family: int = 0,
892+
type: int = 0,
893+
proto: int = 0,
894+
flags: int = 0,
879895
) -> None:
880896
cancel_scope.cancel()
881897
await _core.checkpoint()

src/trio/_tests/test_testing_raisesgroup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import re
44
import sys
55
from types import TracebackType
6-
from typing import Any
76

87
import pytest
98

@@ -235,10 +234,9 @@ def test_RaisesGroup_matches() -> None:
235234

236235

237236
def test_message() -> None:
238-
# Explicit "Any" is not allowed
239-
def check_message( # type: ignore[misc]
237+
def check_message(
240238
message: str,
241-
body: RaisesGroup[Any],
239+
body: RaisesGroup[object],
242240
) -> None:
243241
with pytest.raises(
244242
AssertionError,

0 commit comments

Comments
 (0)