Skip to content

Commit 092a3e2

Browse files
johntrandallclaude
andcommitted
Fix KeyError when active_user_count is missing from API response
Reddit removed the active_user_count field from some subreddit API endpoints. The Subreddit model constructor used bracket access (d['active_user_count']) which raises KeyError when the field is absent. Changed to d.get('active_user_count') which returns None for missing keys, falling through to the existing -1 default. Added tests for Subreddit model construction covering: - Complete API response (field present with value) - Field present but None (search results) - Field absent (new Reddit API behavior) - Field present with zero value Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c117c4e commit 092a3e2

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

redditwarp/models/subreddit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def __init__(self, d: Mapping[str, Any]) -> None:
212212

213213
self.subscriber_count: int = d['subscribers']
214214
("")
215-
self.viewing_count: int = -1 if (x := d['active_user_count']) is None else x
215+
self.viewing_count: int = -1 if (x := d.get('active_user_count')) is None else x
216216
("""
217217
The number of online users who are subscribed to the subreddit.
218218

tests/models/__init__.py

Whitespace-only changes.

tests/models/test_subreddit.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
from __future__ import annotations
3+
4+
import pytest
5+
6+
from redditwarp.models.subreddit import Subreddit
7+
8+
9+
def _make_subreddit_data(**overrides) -> dict:
10+
"""Build a minimal subreddit API response dict.
11+
12+
All required fields are populated with sensible defaults.
13+
Pass keyword arguments to override or remove specific fields.
14+
"""
15+
data = {
16+
'id': '2qh1i',
17+
'display_name': 'AskReddit',
18+
'subreddit_type': 'public',
19+
'created_utc': 1201233135.0,
20+
'title': 'Ask Reddit...',
21+
'public_description': 'A subreddit for asking questions.',
22+
'public_description_html': '<p>A subreddit for asking questions.</p>',
23+
'description': 'Rules and sidebar text.',
24+
'description_html': '<p>Rules and sidebar text.</p>',
25+
'submit_text': '',
26+
'submit_text_html': None,
27+
'submit_text_label': None,
28+
'submit_link_label': None,
29+
'subscribers': 48000000,
30+
'active_user_count': 15000,
31+
'submission_type': 'any',
32+
'allow_galleries': True,
33+
'allow_polls': True,
34+
'suggested_comment_sort': None,
35+
'over18': False,
36+
'quarantine': False,
37+
'user_is_moderator': None,
38+
'user_is_subscriber': None,
39+
'user_flair_enabled_in_sr': None,
40+
'user_sr_flair_enabled': None,
41+
'user_flair_text': None,
42+
'user_flair_css_class': None,
43+
'user_flair_background_color': None,
44+
'user_flair_text_color': None,
45+
'user_flair_template_id': None,
46+
'can_assign_user_flair': False,
47+
'can_assign_link_flair': False,
48+
'user_flair_position': 'right',
49+
'link_flair_enabled': True,
50+
'link_flair_position': 'left',
51+
'has_menu_widget': False,
52+
}
53+
data.update(overrides)
54+
return data
55+
56+
57+
class TestSubredditConstruction:
58+
"""Test Subreddit model construction from API response dicts."""
59+
60+
def test_basic_construction(self):
61+
"""Subreddit can be constructed from a complete API response."""
62+
data = _make_subreddit_data()
63+
subr = Subreddit(data)
64+
assert subr.name == 'AskReddit'
65+
assert subr.subscriber_count == 48000000
66+
assert subr.viewing_count == 15000
67+
68+
def test_active_user_count_none(self):
69+
"""viewing_count is -1 when active_user_count is None (e.g., search results)."""
70+
data = _make_subreddit_data(active_user_count=None)
71+
subr = Subreddit(data)
72+
assert subr.viewing_count == -1
73+
74+
def test_active_user_count_missing(self):
75+
"""viewing_count is -1 when active_user_count is absent from API response.
76+
77+
Reddit removed this field from some API endpoints circa 2025.
78+
Previously this raised KeyError.
79+
"""
80+
data = _make_subreddit_data()
81+
del data['active_user_count']
82+
subr = Subreddit(data)
83+
assert subr.viewing_count == -1
84+
85+
def test_active_user_count_zero(self):
86+
"""viewing_count is 0 when active_user_count is 0."""
87+
data = _make_subreddit_data(active_user_count=0)
88+
subr = Subreddit(data)
89+
assert subr.viewing_count == 0

0 commit comments

Comments
 (0)