Skip to content

Commit 6a7650d

Browse files
committed
duplicate validation logic, bump pyright
1 parent 8e863b8 commit 6a7650d

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/trio/_tests/test_timeouts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ async def test_timeouts_raise_value_error() -> None:
166166
):
167167
with pytest.raises(
168168
ValueError,
169-
match="^(duration|deadline|timeout) must (not )*be (non-negative|NaN)$",
169+
match="^(deadline|`seconds`) must (not )*be (non-negative|NaN)$",
170170
):
171171
await fun(val)
172172

@@ -180,7 +180,7 @@ async def test_timeouts_raise_value_error() -> None:
180180
):
181181
with pytest.raises(
182182
ValueError,
183-
match="^(duration|deadline|timeout|relative deadline) must (not )*be (non-negative|NaN)$",
183+
match="^(deadline|`seconds`) must (not )*be (non-negative|NaN)$",
184184
):
185185
with cm(val):
186186
pass # pragma: no cover

src/trio/_timeouts.py

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

3+
import math
34
import sys
45
from contextlib import contextmanager
56
from typing import TYPE_CHECKING
@@ -23,6 +24,7 @@ def move_on_at(deadline: float, *, shield: bool = False) -> trio.CancelScope:
2324
ValueError: if deadline is NaN.
2425
2526
"""
27+
# CancelScope validates that deadline isn't math.nan
2628
return trio.CancelScope(deadline=deadline, shield=shield)
2729

2830

@@ -42,9 +44,14 @@ def move_on_after(
4244
of the newly created cancel scope.
4345
4446
Raises:
45-
ValueError: if timeout is less than zero or NaN.
47+
ValueError: if `seconds` is less than zero or NaN.
4648
4749
"""
50+
# duplicate validation logic to have the correct parameter name
51+
if seconds < 0:
52+
raise ValueError("`seconds` must be non-negative")
53+
if math.isnan(seconds):
54+
raise ValueError("`seconds` must not be NaN")
4855
return trio.CancelScope(
4956
shield=shield,
5057
relative_deadline=seconds,
@@ -92,7 +99,7 @@ async def sleep(seconds: float) -> None:
9299
93100
"""
94101
if seconds < 0:
95-
raise ValueError("duration must be non-negative")
102+
raise ValueError("`seconds` must be non-negative")
96103
if seconds == 0:
97104
await trio.lowlevel.checkpoint()
98105
else:

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pylint==3.2.7
105105
# via -r test-requirements.in
106106
pyopenssl==24.2.1
107107
# via -r test-requirements.in
108-
pyright==1.1.378
108+
pyright==1.1.381
109109
# via -r test-requirements.in
110110
pytest==8.3.2
111111
# via -r test-requirements.in

0 commit comments

Comments
 (0)