Commit 704dfb4
fix: Order.type rename, BASE_URL validation, Retry-After NaN/negative guard (#112)
* fix(orders, events, series): rename Order.type, normalize bool query params
F-N-03: Order.type was leftover from the pre-v0.8.0 CreateOrderRequest.type
field. The spec's Order schema does still include a `type` enum on
responses, so rename SDK-side to `order_type` (validation alias accepts
the wire `type`) — same builtin-shadow rationale as milestone_type,
target_type, and incentive_type elsewhere.
F-N-08: events.py and series.py built bool query params inline with
`"true" if x else None`, which silently drops explicit False. Replace
with the documented `_bool_param(...)` helper so callers can opt out
(False -> "false"). Mechanical, matches markets.py / live_data.py.
Closes #91
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(config): validate KALSHI_API_BASE_URL scheme and host
Reject base_url with a non-http(s) scheme or http:// pointing at a
remote host. The previous config accepted any string, so an attacker
who could write to a process's environment (docker run -e, CI variable,
shell history) could redirect every authenticated request — and the
KALSHI-ACCESS-KEY header and request signature — to an endpoint they
control.
- https://* with a known Kalshi host (production, demo): silent.
- https://* with any other host: warn (keeps legitimate proxy use
cases working but surfaces awareness).
- http://localhost, 127.0.0.1, ::1: allowed (local mocks, tests).
- http://<remote host>: ValueError at config construction.
- non-http(s) scheme or missing host: ValueError.
Closes #94
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(retry): reject negative, NaN, and infinite Retry-After values
Retry-After was parsed with bare float() and used straight in
``min(retry_after, retry_max_delay)``. Three failure modes the cap
didn't cover:
- ``Retry-After: -1`` -> ``min(-1, 30) = -1`` -> ``time.sleep(-1)`` is
a POSIX no-op; the client busy-loops the server-controlled retry.
- ``Retry-After: nan`` -> ``min(nan, 30) = nan`` -> ``time.sleep(nan)``
raises ValueError outside the documented retry pathway.
- ``Retry-After: inf`` -> would survive the cap math but had no test.
Reject all three at parse time and fall back to computed backoff.
HTTP-date format still falls through unchanged.
Closes #96
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* review: address #112 bot feedback (ws_base_url, CHANGELOG, comment trim)
Per code review on PR #112:
- Validate ws_base_url with the same scheme/host rules as base_url.
WebSocket connect carries KALSHI-ACCESS-KEY too, so plaintext-to-remote
is the same credential-leakage surface as #94. Generalized
_validate_base_url -> _validate_url(secure=, plaintext=) so http/https
and ws/wss share the rule.
- CHANGELOG.md: added [Unreleased] Breaking section for Order.type ->
Order.order_type. Version bump (v1.2 vs v2.0) still deferred to
release cut, but the entry now exists.
- _base_client.py: trimmed the 5-line Retry-After comment block to one
line per CLAUDE.md style guide.
- Added TestWsBaseUrlValidation (+6 tests).
Deferred (not addressed):
- Optional @Property def type(self) deprecation shim. User explicitly
picked the clean-break rename earlier.
- Pre-existing Order lacks model_config; outside this PR's scope, will
open a follow-up issue.
- Cosmetic host -> url_host test variable rename; low value.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(retry): honor Retry-After: 0 end-to-end through transport
Per second-pass bot review on #112: the prior commit kept Retry-After: 0
on the error object, but transport's "if error.retry_after:" check
dropped 0 as falsy and fell through to backoff. Switched to
"is not None" in both sync and async transport. Added regression
tests that monkeypatch time.sleep / asyncio.sleep and assert the
delay is exactly 0.0 (not the backoff fallback).
Also addressed:
- config._validate_url docstring trimmed to one line (CLAUDE.md).
- Called via KalshiConfig._validate_url(...) rather than self.
(staticmethod-through-self hides that no instance state is used).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent ca0d125 commit 704dfb4
11 files changed
Lines changed: 384 additions & 16 deletions
File tree
- kalshi
- models
- resources
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
5 | 29 | | |
6 | 30 | | |
7 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
168 | 172 | | |
169 | 173 | | |
170 | 174 | | |
171 | | - | |
172 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
173 | 181 | | |
174 | 182 | | |
175 | 183 | | |
| |||
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
285 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
286 | 298 | | |
287 | 299 | | |
288 | 300 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
17 | 26 | | |
18 | 27 | | |
19 | 28 | | |
| |||
42 | 51 | | |
43 | 52 | | |
44 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
45 | 89 | | |
46 | 90 | | |
47 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
48 | 55 | | |
49 | 56 | | |
50 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
| 29 | + | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
233 | | - | |
| 233 | + | |
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
| 30 | + | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
| 191 | + | |
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
74 | 99 | | |
75 | 100 | | |
76 | 101 | | |
| |||
0 commit comments