Skip to content

Commit 45ddb25

Browse files
lesnik512claude
andcommitted
fix: validate Timeout/Limits negatives in __post_init__
Both dataclasses now raise ValueError on construction if any field is negative. Zero is permitted (Timeout zero = fail-immediately sentinel; Limits zero = downstream's call on what it means, typically "no limit"). Closes deferred-work entry: "Timeout / Limits negative-value validation". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b877a27 commit 45ddb25

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/httpware/config.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Timeout:
1717
write: float = 30.0
1818
pool: float = 5.0
1919

20+
def __post_init__(self) -> None:
21+
for attr in ("connect", "read", "write", "pool"):
22+
value = getattr(self, attr)
23+
if value < 0:
24+
msg = f"Timeout.{attr} must be non-negative (got {value})"
25+
raise ValueError(msg)
26+
2027

2128
@dataclass(frozen=True, slots=True)
2229
class Limits:
@@ -26,6 +33,17 @@ class Limits:
2633
max_keepalive_connections: int = 20
2734
keepalive_expiry: float = 5.0
2835

36+
def __post_init__(self) -> None:
37+
if self.max_connections < 0:
38+
msg = f"max_connections must be non-negative (got {self.max_connections})"
39+
raise ValueError(msg)
40+
if self.max_keepalive_connections < 0:
41+
msg = f"max_keepalive_connections must be non-negative (got {self.max_keepalive_connections})"
42+
raise ValueError(msg)
43+
if self.keepalive_expiry < 0:
44+
msg = f"keepalive_expiry must be non-negative (got {self.keepalive_expiry})"
45+
raise ValueError(msg)
46+
2947

3048
@dataclass(frozen=True, slots=True)
3149
class ClientConfig:

‎tests/test_config.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,29 @@ def test_client_config_is_frozen() -> None:
5050
cfg = ClientConfig()
5151
with pytest.raises(FrozenInstanceError):
5252
cfg.base_url = "https://example.com" # ty: ignore[invalid-assignment]
53+
54+
55+
@pytest.mark.parametrize("field", ["connect", "read", "write", "pool"])
56+
def test_timeout_rejects_negative(field: str) -> None:
57+
with pytest.raises(ValueError, match=rf"Timeout\.{field} must be non-negative"):
58+
Timeout(**{field: -1.0})
59+
60+
61+
def test_timeout_accepts_zero() -> None:
62+
# Zero is a valid sentinel (fail immediately on this phase).
63+
Timeout(connect=0.0, read=0.0, write=0.0, pool=0.0)
64+
65+
66+
@pytest.mark.parametrize("field", ["max_connections", "max_keepalive_connections"])
67+
def test_limits_rejects_negative_int(field: str) -> None:
68+
with pytest.raises(ValueError, match=f"{field} must be non-negative"):
69+
Limits(**{field: -1})
70+
71+
72+
def test_limits_rejects_negative_keepalive_expiry() -> None:
73+
with pytest.raises(ValueError, match="keepalive_expiry must be non-negative"):
74+
Limits(keepalive_expiry=-0.5)
75+
76+
77+
def test_limits_accepts_zero() -> None:
78+
Limits(max_connections=0, max_keepalive_connections=0, keepalive_expiry=0.0)

0 commit comments

Comments
 (0)