From 13b1a47f643d86d6e30d82f462c7d75a5ba5b076 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Wed, 11 Nov 2026 18:40:53 -0500 Subject: [PATCH 1/4] Stop accepting nan as much. --- newsfragments/3578.removal.rst | 1 + src/trio/_core/_mock_clock.py | 15 +++++++++++---- src/trio/_core/_run.py | 2 ++ src/trio/_core/_tests/test_mock_clock.py | 8 ++------ src/trio/_dtls.py | 3 +++ src/trio/_highlevel_open_tcp_stream.py | 3 +++ 6 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 newsfragments/3578.removal.rst diff --git a/newsfragments/3578.removal.rst b/newsfragments/3578.removal.rst new file mode 100644 index 0000000000..b55b241992 --- /dev/null +++ b/newsfragments/3578.removal.rst @@ -0,0 +1 @@ +Trio will no longer accept `math.nan` in many places where floats are accepted. diff --git a/src/trio/_core/_mock_clock.py b/src/trio/_core/_mock_clock.py index bc530115f5..7bcd8b4e2d 100644 --- a/src/trio/_core/_mock_clock.py +++ b/src/trio/_core/_mock_clock.py @@ -87,14 +87,16 @@ def rate(self) -> float: @rate.setter def rate(self, new_rate: float) -> None: - if isnan(new_rate) or new_rate < 0: + if isnan(new_rate): + raise ValueError("rate must not be NaN") + elif new_rate < 0: raise ValueError(f"rate must be a non-negative number, not {new_rate!r}") else: real = self._real_clock() virtual = self._real_to_virtual(real) self._virtual_base = virtual self._real_base = real - self._rate = float(new_rate) + self._rate = new_rate @property def autojump_threshold(self) -> float: @@ -102,8 +104,11 @@ def autojump_threshold(self) -> float: @autojump_threshold.setter def autojump_threshold(self, new_autojump_threshold: float) -> None: - self._autojump_threshold = float(new_autojump_threshold) - self._try_resync_autojump_threshold() + if isnan(new_autojump_threshold): + raise ValueError("autojump_threshold must not be NaN") + else: + self._autojump_threshold = new_autojump_threshold + self._try_resync_autojump_threshold() # runner.clock_autojump_threshold is an internal API that isn't easily # usable by custom third-party Clock objects. If you need access to this @@ -160,6 +165,8 @@ def jump(self, seconds: float) -> None: ValueError: if you try to pass a negative value for ``seconds``. """ + if isnan(seconds): + raise ValueError("time must not be NaN") if seconds < 0: raise ValueError("time can't go backwards") self._virtual_base += seconds diff --git a/src/trio/_core/_run.py b/src/trio/_core/_run.py index ddd4ea7ee2..a1d1fb0057 100644 --- a/src/trio/_core/_run.py +++ b/src/trio/_core/_run.py @@ -2305,6 +2305,8 @@ async def test_lock_fairness(): print("FAIL") """ + if isnan(cushion): + raise ValueError("wait_all_tasks_blocked's cushion must not be NaN") task = current_task() key = (cushion, id(task)) self.waiting_for_idle[key] = task diff --git a/src/trio/_core/_tests/test_mock_clock.py b/src/trio/_core/_tests/test_mock_clock.py index 47ee1ff2a4..42b43406e2 100644 --- a/src/trio/_core/_tests/test_mock_clock.py +++ b/src/trio/_core/_tests/test_mock_clock.py @@ -34,15 +34,11 @@ def test_mock_clock() -> None: c.rate = -1 assert c.rate == 0 - with pytest.raises( - ValueError, match=r"^rate must be a non-negative number, not nan$" - ): + with pytest.raises(ValueError, match=r"^rate must not be NaN$"): c.rate = float("nan") assert c.rate == 0 - with pytest.raises( - ValueError, match=r"^rate must be a non-negative number, not nan$" - ): + with pytest.raises(ValueError, match=r"^rate must not be NaN$"): MockClock(rate=float("nan")) c.rate = 2 diff --git a/src/trio/_dtls.py b/src/trio/_dtls.py index a789038257..ab27f9cec7 100644 --- a/src/trio/_dtls.py +++ b/src/trio/_dtls.py @@ -17,6 +17,7 @@ import warnings import weakref from itertools import count +from math import isnan from typing import TYPE_CHECKING, Generic, TypeAlias, TypeVar from weakref import ReferenceType, WeakValueDictionary @@ -957,6 +958,8 @@ async def do_handshake(self, *, initial_retransmit_timeout: float = 1.0) -> None network. """ + if isnan(initial_retransmit_timeout): + raise ValueError("timeout must not be NaN") async with self._handshake_lock: if self._did_handshake: return diff --git a/src/trio/_highlevel_open_tcp_stream.py b/src/trio/_highlevel_open_tcp_stream.py index 400bc52834..29c350503c 100644 --- a/src/trio/_highlevel_open_tcp_stream.py +++ b/src/trio/_highlevel_open_tcp_stream.py @@ -2,6 +2,7 @@ import sys from contextlib import contextmanager, suppress +from math import isnan from typing import TYPE_CHECKING import trio @@ -268,6 +269,8 @@ async def open_tcp_stream( if happy_eyeballs_delay is None: happy_eyeballs_delay = DEFAULT_DELAY + if isnan(happy_eyeballs_delay): + raise ValueError("happy eyeballs delay must not be NaN") targets = await getaddrinfo(host, port, type=SOCK_STREAM) From 8a9c1e1e579a4334ab36aa0cbc1d2504dc4a19fa Mon Sep 17 00:00:00 2001 From: A5rocks Date: Wed, 11 Nov 2026 19:46:07 -0500 Subject: [PATCH 2/4] Add tests --- src/trio/_core/_run.py | 2 +- src/trio/_core/_tests/test_mock_clock.py | 10 ++++++++++ src/trio/_tests/test_dtls.py | 9 +++++++++ src/trio/_tests/test_highlevel_open_tcp_stream.py | 13 +++++++++++++ src/trio/_tests/test_testing.py | 5 +++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/trio/_core/_run.py b/src/trio/_core/_run.py index a1d1fb0057..61f670cab6 100644 --- a/src/trio/_core/_run.py +++ b/src/trio/_core/_run.py @@ -2306,7 +2306,7 @@ async def test_lock_fairness(): """ if isnan(cushion): - raise ValueError("wait_all_tasks_blocked's cushion must not be NaN") + raise ValueError("cushion must not be NaN") task = current_task() key = (cushion, id(task)) self.waiting_for_idle[key] = task diff --git a/src/trio/_core/_tests/test_mock_clock.py b/src/trio/_core/_tests/test_mock_clock.py index 42b43406e2..ca187d3855 100644 --- a/src/trio/_core/_tests/test_mock_clock.py +++ b/src/trio/_core/_tests/test_mock_clock.py @@ -41,6 +41,16 @@ def test_mock_clock() -> None: with pytest.raises(ValueError, match=r"^rate must not be NaN$"): MockClock(rate=float("nan")) + with pytest.raises(ValueError, match=r"^autojump_threshold must not be NaN$"): + c.autojump_threshold = float("nan") + assert c.autojump_threshold == inf + + with pytest.raises(ValueError, match=r"^autojump_threshold must not be NaN$"): + MockClock(autojump_threshold=float("nan")) + + with pytest.raises(ValueError, match=r"^time must not be NaN$"): + c.jump(float("nan")) + c.rate = 2 assert c.current_time() == 1.2 REAL_NOW += 1 diff --git a/src/trio/_tests/test_dtls.py b/src/trio/_tests/test_dtls.py index 5a34d8c811..1bb7284914 100644 --- a/src/trio/_tests/test_dtls.py +++ b/src/trio/_tests/test_dtls.py @@ -706,6 +706,15 @@ def route_packet(packet: UDPPacket) -> None: after = trio.current_time() assert after - before == t + with pytest.raises(ValueError, match=r"^timeout must not be NaN$"): + with endpoint() as client: + before = trio.current_time() + blackholed = True + channel = client.connect(address, client_ctx_fn()) + await channel.do_handshake(initial_retransmit_timeout=float("nan")) + after = trio.current_time() + assert after - before == 0 + async def test_explicit_tiny_mtu_is_respected( server_ctx: SSL.Context, client_ctx: SSL.Context diff --git a/src/trio/_tests/test_highlevel_open_tcp_stream.py b/src/trio/_tests/test_highlevel_open_tcp_stream.py index 3c9ae9e593..7aecbdce51 100644 --- a/src/trio/_tests/test_highlevel_open_tcp_stream.py +++ b/src/trio/_tests/test_highlevel_open_tcp_stream.py @@ -509,6 +509,19 @@ async def test_none_default(autojump_clock: MockClock) -> None: } +async def test_happy_eyeballs_nan_delay() -> None: + with pytest.raises(ValueError, match=r"^happy eyeballs delay must not be NaN$"): + 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=float("nan"), + ) + + async def test_custom_errors_expedite(autojump_clock: MockClock) -> None: sock, scenario = await run_scenario( 80, diff --git a/src/trio/_tests/test_testing.py b/src/trio/_tests/test_testing.py index 7093a6187e..cdb73fda4c 100644 --- a/src/trio/_tests/test_testing.py +++ b/src/trio/_tests/test_testing.py @@ -107,6 +107,11 @@ async def wait_big_cushion() -> None: ] +async def test_wait_all_tasks_blocked_with_nan_cushion() -> None: + with pytest.raises(ValueError, match=r"^cushion must not be NaN$"): + await wait_all_tasks_blocked(float("nan")) + + ################################################################ From a3a61480293d4327477e89e4d9a9ff9660a3ec03 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Fri, 13 Nov 2026 09:46:02 -0500 Subject: [PATCH 3/4] Review comment --- src/trio/_core/_mock_clock.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/trio/_core/_mock_clock.py b/src/trio/_core/_mock_clock.py index 7bcd8b4e2d..8d457a5102 100644 --- a/src/trio/_core/_mock_clock.py +++ b/src/trio/_core/_mock_clock.py @@ -106,9 +106,9 @@ def autojump_threshold(self) -> float: def autojump_threshold(self, new_autojump_threshold: float) -> None: if isnan(new_autojump_threshold): raise ValueError("autojump_threshold must not be NaN") - else: - self._autojump_threshold = new_autojump_threshold - self._try_resync_autojump_threshold() + + self._autojump_threshold = new_autojump_threshold + self._try_resync_autojump_threshold() # runner.clock_autojump_threshold is an internal API that isn't easily # usable by custom third-party Clock objects. If you need access to this From 089c9797c93276f1bbfb960e9ceee27473ea11a3 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Fri, 13 Nov 2026 09:51:56 -0500 Subject: [PATCH 4/4] Fix coverage --- src/trio/_tests/test_dtls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/trio/_tests/test_dtls.py b/src/trio/_tests/test_dtls.py index 1bb7284914..ea77822197 100644 --- a/src/trio/_tests/test_dtls.py +++ b/src/trio/_tests/test_dtls.py @@ -712,8 +712,8 @@ def route_packet(packet: UDPPacket) -> None: blackholed = True channel = client.connect(address, client_ctx_fn()) await channel.do_handshake(initial_retransmit_timeout=float("nan")) - after = trio.current_time() - assert after - before == 0 + after = trio.current_time() + assert after - before == 0 async def test_explicit_tiny_mtu_is_respected(