Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/3588.deprecated.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate using `None` for `open_tcp_stream`'s ``happy_eyeball_delay``. Instead, you can pass ``0.25``.
17 changes: 12 additions & 5 deletions src/trio/_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import trio
from trio.socket import SOCK_STREAM, SocketType, getaddrinfo, socket

from ._deprecate import warn_deprecated

if TYPE_CHECKING:
from collections.abc import Generator, MutableSequence
from socket import AddressFamily, SocketKind
Expand Down Expand Up @@ -186,7 +188,7 @@ async def open_tcp_stream(
host: str | bytes,
port: int,
*,
happy_eyeballs_delay: float | None = DEFAULT_DELAY,
happy_eyeballs_delay: float = DEFAULT_DELAY,
local_address: str | None = None,
) -> trio.SocketStream:
"""Connect to the given host and port over TCP.
Expand Down Expand Up @@ -226,9 +228,9 @@ async def open_tcp_stream(

port (int): The port to connect to.

happy_eyeballs_delay (float or None): How many seconds to wait for each
happy_eyeballs_delay (float or int): How many seconds to wait for each
connection attempt to succeed or fail before getting impatient and
starting another one in parallel. Set to `None` if you want
starting another one in parallel. Set to `math.inf` if you want
to limit to only one connection attempt at a time (like
:func:`socket.create_connection`). Default: 0.25 (250 ms).

Expand Down Expand Up @@ -265,8 +267,13 @@ async def open_tcp_stream(
raise ValueError(f"host must be str or bytes, not {host!r}")
if not isinstance(port, int):
raise TypeError(f"port must be int, not {port!r}")

if happy_eyeballs_delay is None:
if not isinstance(happy_eyeballs_delay, (int, float)):
warn_deprecated(
"non-int, non-float happy_eyeballs_delay",
version="0.36.0",
instead="pass `math.inf` or 0.25",
issue="3588",
)
happy_eyeballs_delay = DEFAULT_DELAY

targets = await getaddrinfo(host, port, type=SOCK_STREAM)
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_highlevel_ssl_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def open_ssl_over_tcp_stream(
*,
https_compatible: bool = False,
ssl_context: ssl.SSLContext | None = None,
happy_eyeballs_delay: float | None = DEFAULT_DELAY,
happy_eyeballs_delay: float = DEFAULT_DELAY,
) -> trio.SSLStream[SocketStream]:
"""Make a TLS-encrypted Connection to the given host and port over TCP.

Expand Down
33 changes: 12 additions & 21 deletions src/trio/_tests/test_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ async def run_scenario(
# If this is True, we require there to be an exception, and return
# (exception, scenario object)
expect_error: tuple[type[BaseException], ...] | type[BaseException] = (),
happy_eyeballs_delay: float | None = 0.25,
happy_eyeballs_delay: float = 0.25,
local_address: str | None = None,
) -> tuple[SocketType, Scenario] | tuple[BaseException, Scenario]:
supported_families = set()
Expand Down Expand Up @@ -487,26 +487,17 @@ async def test_custom_delay(autojump_clock: MockClock) -> None:
}


async def test_none_default(autojump_clock: MockClock) -> None:
"""Copy of test_basic_fallthrough, but specifying the delay =None"""
sock, scenario = await run_scenario(
80,
[
("1.1.1.1", 1, "success"),
("2.2.2.2", 1, "success"),
("3.3.3.3", 0.2, "success"),
],
happy_eyeballs_delay=None,
)
assert isinstance(sock, FakeSocket)
assert sock.ip == "3.3.3.3"
# current time is default time + default time + connection time
assert trio.current_time() == (0.250 + 0.250 + 0.2)
assert scenario.connect_times == {
"1.1.1.1": 0,
"2.2.2.2": 0.250,
"3.3.3.3": 0.500,
}
async def test_none_happy_eyeballs_delay(autojump_clock: MockClock) -> None:
with pytest.deprecated_call():
await run_scenario(
80,
[
("1.1.1.1", 1, "success"),
("2.2.2.2", 1, "success"),
("3.3.3.3", 0.2, "success"),
],
happy_eyeballs_delay=None, # type: ignore[arg-type]
)


async def test_custom_errors_expedite(autojump_clock: MockClock) -> None:
Expand Down
Loading