diff --git a/newsfragments/3588.deprecated.rst b/newsfragments/3588.deprecated.rst new file mode 100644 index 0000000000..0db1b916e8 --- /dev/null +++ b/newsfragments/3588.deprecated.rst @@ -0,0 +1 @@ +Deprecate using `None` for `open_tcp_stream`'s ``happy_eyeball_delay``. Instead, you can pass ``0.25``. diff --git a/src/trio/_highlevel_open_tcp_stream.py b/src/trio/_highlevel_open_tcp_stream.py index 400bc52834..e6e987b87e 100644 --- a/src/trio/_highlevel_open_tcp_stream.py +++ b/src/trio/_highlevel_open_tcp_stream.py @@ -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 @@ -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. @@ -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). @@ -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) diff --git a/src/trio/_highlevel_ssl_helpers.py b/src/trio/_highlevel_ssl_helpers.py index 1239491a43..3166172568 100644 --- a/src/trio/_highlevel_ssl_helpers.py +++ b/src/trio/_highlevel_ssl_helpers.py @@ -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. diff --git a/src/trio/_tests/test_highlevel_open_tcp_stream.py b/src/trio/_tests/test_highlevel_open_tcp_stream.py index 3c9ae9e593..0cf8c8aa6b 100644 --- a/src/trio/_tests/test_highlevel_open_tcp_stream.py +++ b/src/trio/_tests/test_highlevel_open_tcp_stream.py @@ -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() @@ -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: