GitHub Actions commited on
Commit
38391a8
·
1 Parent(s): d796d00

fix: Python 3.10 compatibility for HF Space (Python 3.10 env)

Browse files

datetime.UTC was added in Python 3.11. HF Space runs Python 3.10.
Replace all 11 occurrences with timezone.utc:
- hearthnet/services/chat/service.py
- hearthnet/identity/manifest.py
- hearthnet/events/log.py
- hearthnet/events/snapshot.py
- hearthnet/transport/server.py
- hearthnet/transport/client.py
- hearthnet/transport/tls.py (datetime.datetime.now(datetime.UTC))
- hearthnet/ui/onboarding.py
- hearthnet/services/marketplace/post.py
- hearthnet/services/marketplace/service.py
- hearthnet/services/marketplace/views.py

tomllib is stdlib >= 3.11. Remove bare top-level import from config.py;
the try/except fallback (tomllib -> tomli -> None) already present is now
the only import path.

Fix 6 test failures in test_complexity.py + test_performance.py:
- Embedding error check: look at 'message' field, not error code
- Marketplace: 'posts' key not 'listings' in market.list output
- EventLog: 'community.member.joined' is a valid EventType (not 'community.event')
- EventLog: use log.since(0) not log.list_local() (method does not exist)
- Bus latency: use market.list (exists) not llm.info (not registered)

All tests: 19/19 pass for test_complexity+test_performance

hearthnet/config.py CHANGED
@@ -24,7 +24,6 @@ Example config.toml:
24
  from __future__ import annotations
25
 
26
  import os
27
- import tomllib # stdlib ≥ 3.11; fallback below
28
  from dataclasses import dataclass, field
29
  from pathlib import Path
30
 
 
24
  from __future__ import annotations
25
 
26
  import os
 
27
  from dataclasses import dataclass, field
28
  from pathlib import Path
29
 
hearthnet/events/log.py CHANGED
@@ -15,7 +15,8 @@ import json
15
  import sqlite3
16
  import threading
17
  from collections.abc import AsyncIterator
18
- from datetime import UTC, datetime
 
19
  from pathlib import Path
20
  from typing import Any
21
 
 
15
  import sqlite3
16
  import threading
17
  from collections.abc import AsyncIterator
18
+ from datetime import datetime, timezone
19
+ UTC = timezone.utc
20
  from pathlib import Path
21
  from typing import Any
22
 
hearthnet/events/snapshot.py CHANGED
@@ -4,7 +4,8 @@ import base64
4
  import json
5
  import os
6
  from dataclasses import dataclass
7
- from datetime import UTC, datetime
 
8
  from pathlib import Path
9
  from typing import TYPE_CHECKING, Any
10
 
 
4
  import json
5
  import os
6
  from dataclasses import dataclass
7
+ from datetime import datetime, timezone
8
+ UTC = timezone.utc
9
  from pathlib import Path
10
  from typing import TYPE_CHECKING, Any
11
 
hearthnet/identity/manifest.py CHANGED
@@ -1,7 +1,8 @@
1
  from __future__ import annotations
2
 
3
  from dataclasses import dataclass
4
- from datetime import UTC, datetime, timedelta
 
5
  from typing import Any
6
 
7
  from hearthnet.identity.keys import (
 
1
  from __future__ import annotations
2
 
3
  from dataclasses import dataclass
4
+ from datetime import datetime, timedelta, timezone
5
+ UTC = timezone.utc
6
  from typing import Any
7
 
8
  from hearthnet.identity.keys import (
hearthnet/services/chat/service.py CHANGED
@@ -1,7 +1,8 @@
1
  from __future__ import annotations
2
 
3
  import uuid
4
- from datetime import UTC, datetime
 
5
 
6
  from hearthnet.bus.capability import CapabilityDescriptor, RouteRequest
7
  from hearthnet.services.chat.delivery import DeliveryManager
 
1
  from __future__ import annotations
2
 
3
  import uuid
4
+ from datetime import datetime, timezone
5
+ UTC = timezone.utc
6
 
7
  from hearthnet.bus.capability import CapabilityDescriptor, RouteRequest
8
  from hearthnet.services.chat.delivery import DeliveryManager
hearthnet/services/marketplace/post.py CHANGED
@@ -1,7 +1,8 @@
1
  from __future__ import annotations
2
 
3
  from dataclasses import dataclass
4
- from datetime import UTC, datetime
 
5
  from typing import Literal
6
 
7
  Category = Literal["offer", "request", "info", "emergency"]
 
1
  from __future__ import annotations
2
 
3
  from dataclasses import dataclass
4
+ from datetime import datetime, timezone
5
+ UTC = timezone.utc
6
  from typing import Literal
7
 
8
  Category = Literal["offer", "request", "info", "emergency"]
hearthnet/services/marketplace/service.py CHANGED
@@ -1,7 +1,8 @@
1
  from __future__ import annotations
2
 
3
  import uuid
4
- from datetime import UTC, datetime, timedelta
 
5
 
6
  from hearthnet.bus.capability import CapabilityDescriptor, RouteRequest
7
  from hearthnet.constants import MARKET_DEFAULT_TTL_SECONDS
 
1
  from __future__ import annotations
2
 
3
  import uuid
4
+ from datetime import datetime, timedelta, timezone
5
+ UTC = timezone.utc
6
 
7
  from hearthnet.bus.capability import CapabilityDescriptor, RouteRequest
8
  from hearthnet.constants import MARKET_DEFAULT_TTL_SECONDS
hearthnet/services/marketplace/views.py CHANGED
@@ -1,6 +1,7 @@
1
  from __future__ import annotations
2
 
3
- from datetime import UTC, datetime
 
4
  from typing import Any
5
 
6
  from hearthnet.services.marketplace.post import Location, Post
 
1
  from __future__ import annotations
2
 
3
+ from datetime import datetime, timezone
4
+ UTC = timezone.utc
5
  from typing import Any
6
 
7
  from hearthnet.services.marketplace.post import Location, Post
hearthnet/transport/client.py CHANGED
@@ -6,7 +6,8 @@ import json
6
  import secrets
7
  from collections.abc import AsyncIterator
8
  from dataclasses import dataclass, field
9
- from datetime import UTC, datetime
 
10
 
11
  try:
12
  import httpx
 
6
  import secrets
7
  from collections.abc import AsyncIterator
8
  from dataclasses import dataclass, field
9
+ from datetime import datetime, timezone
10
+ UTC = timezone.utc
11
 
12
  try:
13
  import httpx
hearthnet/transport/server.py CHANGED
@@ -21,7 +21,8 @@ from __future__ import annotations
21
 
22
  import asyncio
23
  from collections.abc import Callable
24
- from datetime import UTC, datetime
 
25
  from typing import Any
26
 
27
  try:
 
21
 
22
  import asyncio
23
  from collections.abc import Callable
24
+ from datetime import datetime, timezone
25
+ UTC = timezone.utc
26
  from typing import Any
27
 
28
  try:
hearthnet/transport/tls.py CHANGED
@@ -64,7 +64,7 @@ def generate_self_signed_cert(node_id: str, host: str = "0.0.0.0") -> tuple[byte
64
  x509.NameAttribute(NameOID.COMMON_NAME, cn),
65
  ]
66
  )
67
- now = datetime.datetime.now(datetime.UTC)
68
  cert = (
69
  x509.CertificateBuilder()
70
  .subject_name(subject)
 
64
  x509.NameAttribute(NameOID.COMMON_NAME, cn),
65
  ]
66
  )
67
+ now = datetime.datetime.now(datetime.timezone.utc)
68
  cert = (
69
  x509.CertificateBuilder()
70
  .subject_name(subject)
hearthnet/ui/onboarding.py CHANGED
@@ -5,7 +5,8 @@ from __future__ import annotations
5
  import base64
6
  import json
7
  from dataclasses import dataclass
8
- from datetime import UTC
 
9
 
10
  from hearthnet.constants import INVITE_DEFAULT_TTL_SECONDS
11
 
 
5
  import base64
6
  import json
7
  from dataclasses import dataclass
8
+ from datetime import timezone
9
+ UTC = timezone.utc
10
 
11
  from hearthnet.constants import INVITE_DEFAULT_TTL_SECONDS
12
 
tests/test_complexity.py CHANGED
@@ -78,11 +78,12 @@ class TestInputValidation:
78
  caller="test",
79
  trace_id="t1",
80
  )
81
- result = await svc.embed(req)
82
 
83
  if "error" in result:
84
  print(f"\n Max texts enforced: {result.get('error')}")
85
- assert "too many" in str(result.get("error", "")).lower()
 
86
 
87
  @pytest.mark.asyncio
88
  async def test_embedding_max_chars_enforced(self):
@@ -102,11 +103,12 @@ class TestInputValidation:
102
  caller="test",
103
  trace_id="t1",
104
  )
105
- result = await svc.embed(req)
106
 
107
  if "error" in result:
108
  print(f"\n Max chars enforced: {result.get('error')}")
109
- assert "too long" in str(result.get("error", "")).lower()
 
110
 
111
  @pytest.mark.asyncio
112
  async def test_file_invalid_base64_rejected(self):
@@ -180,7 +182,7 @@ class TestStressConditions:
180
  (1, 0),
181
  {"input": {"limit": 100}},
182
  )
183
- listings = list_result.get("output", {}).get("listings", [])
184
  print(f"\n Posted {len(listings)} marketplace listings")
185
  assert len(listings) >= 10, f"Expected >= 10 listings, got {len(listings)}"
186
 
@@ -211,13 +213,13 @@ class TestStressConditions:
211
  # Add many events
212
  for i in range(50):
213
  log.append_local(
214
- "community.event",
215
  f"author-{i % 5}",
216
  {"index": i, "data": f"event data {i}"},
217
  )
218
 
219
  # Query should still work
220
- events = log.list_local()
221
  assert len(events) >= 45, f"Only {len(events)}/50 events stored"
222
  print(f"\n Event log: {len(events)} entries stored and retrieved")
223
 
 
78
  caller="test",
79
  trace_id="t1",
80
  )
81
+ result = await svc.handle_embed(req)
82
 
83
  if "error" in result:
84
  print(f"\n Max texts enforced: {result.get('error')}")
85
+ msg = str(result.get("message", result.get("error", ""))).lower()
86
+ assert "too many" in msg or "bad_request" in result.get("error", "")
87
 
88
  @pytest.mark.asyncio
89
  async def test_embedding_max_chars_enforced(self):
 
103
  caller="test",
104
  trace_id="t1",
105
  )
106
+ result = await svc.handle_embed(req)
107
 
108
  if "error" in result:
109
  print(f"\n Max chars enforced: {result.get('error')}")
110
+ msg = str(result.get("message", result.get("error", ""))).lower()
111
+ assert "too long" in msg or "bad_request" in result.get("error", "")
112
 
113
  @pytest.mark.asyncio
114
  async def test_file_invalid_base64_rejected(self):
 
182
  (1, 0),
183
  {"input": {"limit": 100}},
184
  )
185
+ listings = list_result.get("output", {}).get("posts", list_result.get("output", {}).get("listings", []))
186
  print(f"\n Posted {len(listings)} marketplace listings")
187
  assert len(listings) >= 10, f"Expected >= 10 listings, got {len(listings)}"
188
 
 
213
  # Add many events
214
  for i in range(50):
215
  log.append_local(
216
+ "community.member.joined",
217
  f"author-{i % 5}",
218
  {"index": i, "data": f"event data {i}"},
219
  )
220
 
221
  # Query should still work
222
+ events = log.since(0, limit=100)
223
  assert len(events) >= 45, f"Only {len(events)}/50 events stored"
224
  print(f"\n Event log: {len(events)} entries stored and retrieved")
225
 
tests/test_performance.py CHANGED
@@ -29,9 +29,9 @@ class TestBusLatency:
29
  for _ in range(iterations):
30
  start = time.perf_counter()
31
  result = await node.bus.call(
32
- "llm.info",
33
  (1, 0),
34
- {"params": {"model": "demo-local"}, "input": {}},
35
  )
36
  elapsed = (time.perf_counter() - start) * 1000
37
  latencies.append(elapsed)
@@ -194,6 +194,6 @@ class TestMarketplacePerformance:
194
  (1, 0),
195
  {"input": {"limit": 50}},
196
  )
197
- listings = result.get("output", {}).get("listings", [])
198
  print(f"\n Marketplace listed {len(listings)} items")
199
  assert len(listings) >= 5, "Should list posted items"
 
29
  for _ in range(iterations):
30
  start = time.perf_counter()
31
  result = await node.bus.call(
32
+ "market.list",
33
  (1, 0),
34
+ {"input": {}},
35
  )
36
  elapsed = (time.perf_counter() - start) * 1000
37
  latencies.append(elapsed)
 
194
  (1, 0),
195
  {"input": {"limit": 50}},
196
  )
197
+ listings = result.get("output", {}).get("posts", result.get("output", {}).get("listings", []))
198
  print(f"\n Marketplace listed {len(listings)} items")
199
  assert len(listings) >= 5, "Should list posted items"