recapi 0.4.6__tar.gz → 0.4.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: recapi
3
- Version: 0.4.6
3
+ Version: 0.4.7
4
4
  Summary: Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback.
5
5
  License: MIT
6
6
  Requires-Python: >=3.11
@@ -37,7 +37,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
37
37
  (the dashboard's quick start shows the line with your API's address):
38
38
 
39
39
  ```sh
40
- pip install https://api.<your-domain>/sdk/python/recapi-0.4.6-py3-none-any.whl
40
+ pip install https://api.<your-domain>/sdk/python/recapi-0.4.7-py3-none-any.whl
41
41
  ```
42
42
 
43
43
  To work on the library itself, install it from a checkout of this repository
@@ -27,7 +27,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
27
27
  (the dashboard's quick start shows the line with your API's address):
28
28
 
29
29
  ```sh
30
- pip install https://api.<your-domain>/sdk/python/recapi-0.4.6-py3-none-any.whl
30
+ pip install https://api.<your-domain>/sdk/python/recapi-0.4.7-py3-none-any.whl
31
31
  ```
32
32
 
33
33
  To work on the library itself, install it from a checkout of this repository
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "recapi"
7
- version = "0.4.6"
7
+ version = "0.4.7"
8
8
  description = "Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -29,7 +29,7 @@ from typing import Any, Protocol
29
29
 
30
30
  from .history import History, is_id
31
31
 
32
- __version__ = "0.4.6"
32
+ __version__ = "0.4.7"
33
33
 
34
34
  log = logging.getLogger("recapi")
35
35
  Tags = Sequence[str] | None
@@ -311,11 +311,7 @@ class _UserState:
311
311
  try:
312
312
  data = json.loads(raw)
313
313
  history = History.decode(data["history"])
314
- shown = {
315
- e[0]: list(e[1:])
316
- for e in data["shown"]
317
- if isinstance(e, list) and 3 <= len(e) <= 4 and is_id(e[0])
318
- }
314
+ shown = {e[0]: list(e[1:]) for e in data["shown"] if _is_shown_entry(e)}
319
315
  except (ValueError, KeyError, TypeError):
320
316
  history = None
321
317
  if history is None:
@@ -327,6 +323,18 @@ class _UserState:
327
323
  )
328
324
 
329
325
 
326
+ def _is_shown_entry(e: object) -> bool:
327
+ # Bad types here would raise much later, in track() on the shop's page, or
328
+ # refuse a whole batch of events at the API.
329
+ return (
330
+ isinstance(e, list)
331
+ and 3 <= len(e) <= 4
332
+ and is_id(e[0])
333
+ and isinstance(e[1], str)
334
+ and all(type(t) is int and abs(t) < 2**63 for t in e[2:])
335
+ )
336
+
337
+
330
338
  def _attributable(entry: Sequence[Any], t: int) -> bool:
331
339
  if len(entry) < 3:
332
340
  return t - entry[1] <= DISPLAY_WINDOW_MS
@@ -9,6 +9,7 @@ from __future__ import annotations
9
9
  import copy
10
10
  import math
11
11
  import re
12
+ import sys
12
13
  from typing import Any
13
14
 
14
15
  HALF_LIFE_SECONDS = 604800
@@ -41,7 +42,13 @@ def _is_weight(value: object) -> bool:
41
42
 
42
43
 
43
44
  def _is_time(value: object) -> bool:
44
- return isinstance(value, int | float) and not isinstance(value, bool) and math.isfinite(value)
45
+ # Compared, not converted: a stored 400-digit integer raises OverflowError in
46
+ # math.isfinite, and every later float() would too. NaN and inf fail the bound.
47
+ return (
48
+ isinstance(value, int | float)
49
+ and not isinstance(value, bool)
50
+ and abs(value) <= sys.float_info.max
51
+ )
45
52
 
46
53
 
47
54
  class History:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: recapi
3
- Version: 0.4.6
3
+ Version: 0.4.7
4
4
  Summary: Python client for the recapi recommendation API: per-user history, event batching, retries, popular fallback.
5
5
  License: MIT
6
6
  Requires-Python: >=3.11
@@ -37,7 +37,7 @@ Your recapi API serves the same library, for servers that can't reach PyPI
37
37
  (the dashboard's quick start shows the line with your API's address):
38
38
 
39
39
  ```sh
40
- pip install https://api.<your-domain>/sdk/python/recapi-0.4.6-py3-none-any.whl
40
+ pip install https://api.<your-domain>/sdk/python/recapi-0.4.7-py3-none-any.whl
41
41
  ```
42
42
 
43
43
  To work on the library itself, install it from a checkout of this repository
@@ -577,6 +577,60 @@ class Store(APITest):
577
577
  [e.get("recommendation_id") for e in self.api.events()[0]], [None, None, "rec_1"]
578
578
  )
579
579
 
580
+ def test_a_stored_time_too_big_for_a_float_discards_the_state(self) -> None:
581
+ store = recapi.MemoryStore()
582
+ client = self.client(store=store)
583
+ key = client._key("u", None)
584
+ big = 10**400
585
+ for history in (
586
+ {"top": [["a", 1, big]], "recent": [], "rejected": [], "lastT": None},
587
+ {"top": [["a", big, T0]], "recent": [], "rejected": [], "lastT": None},
588
+ {"top": [], "recent": [["a", big]], "rejected": [], "lastT": None},
589
+ {"top": [], "recent": [], "rejected": [["a", big]], "lastT": None},
590
+ {"top": [], "recent": [], "rejected": [], "lastT": big},
591
+ ):
592
+ with self.subTest(history=history):
593
+ store.set(key, json.dumps({"history": history, "shown": []}))
594
+ with self.assertLogs("recapi", "WARNING"):
595
+ client.track("i", 3, user_id="u")
596
+ self.assertEqual(
597
+ [e[0] for e in json.loads(store.get(key))["history"]["top"]], ["i"]
598
+ )
599
+
600
+ def test_a_stored_entry_of_the_wrong_types_is_dropped(self) -> None:
601
+ store = recapi.MemoryStore()
602
+ client = self.client(store=store)
603
+ key = client._key("u", None)
604
+ for entry in (
605
+ ["i", "rec_1", "oops"],
606
+ ["i", "rec_1", None],
607
+ ["i", "rec_1", T0, "x"],
608
+ ["i", 5, T0],
609
+ ):
610
+ store.set(key, json.dumps({"history": History().encode(), "shown": [entry]}))
611
+ client.track("i", 3, user_id="u")
612
+ client.flush()
613
+ self.assertEqual([e.get("recommendation_id") for e in self.api.events()[0]], [None] * 4)
614
+
615
+ def test_only_a_stored_true_marks_the_history_restored(self) -> None:
616
+ # Anything else asks recapi again: skipping the fetch would lose a
617
+ # returning user's history for good.
618
+ for flag, fetched in ((True, False), (1, True), ("true", True)):
619
+ with self.subTest(flag=flag):
620
+ before = len(self.api.paths())
621
+ store = recapi.MemoryStore()
622
+ client = self.client(store=store)
623
+ history = {
624
+ "top": [["s", 3.0, T0]],
625
+ "recent": [["s", T0]],
626
+ "rejected": [],
627
+ "lastT": T0,
628
+ }
629
+ state = {"history": history, "shown": [], "restored": flag}
630
+ store.set(client._key("u", None), json.dumps(state))
631
+ client.recommend(user_id="u")
632
+ self.assertEqual("/v1/users/u/history" in self.api.paths()[before:], fetched)
633
+
580
634
  def test_the_memory_store_keeps_the_most_recently_used_users(self) -> None:
581
635
  store = recapi.MemoryStore(max_users=2)
582
636
  store.set("a", "1")
@@ -308,6 +308,27 @@ class ClientVectors(APITest):
308
308
  sent = {k: body.get(k) for k in ("history", "recent", "exclude")}
309
309
  self.assertEqual(sent, case["sent"])
310
310
 
311
+ def test_a_stored_entry_of_the_wrong_types_is_dropped(self) -> None:
312
+ vectors = VECTORS["stored_state"]
313
+ self.clock = vectors["now_ms"]
314
+ kept = vectors["kept"]
315
+ for case in vectors["cases"]:
316
+ with self.subTest(case=case):
317
+ self.api.calls.clear()
318
+ store = recapi.MemoryStore()
319
+ client = self.client(store=store)
320
+ shown = [case["entry"], kept]
321
+ store.set(
322
+ client._key("u", None),
323
+ json.dumps({"history": vectors["history"], "shown": shown}),
324
+ )
325
+ with self.assertNoLogs("recapi"):
326
+ client.track("i", 3, user_id="u")
327
+ client.track(kept[0], 3, user_id="u")
328
+ client.flush()
329
+ sent = [e.get("recommendation_id") for b in self.api.events() for e in b]
330
+ self.assertEqual(sent, [case["attributed"], kept[1]])
331
+
311
332
  def test_identify(self) -> None:
312
333
  vectors = VECTORS["identify"]
313
334
  now = vectors["now_ms"]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes