Fix KeyError when active_user_count missing from API response#2
Open
johntrandall wants to merge 1 commit intoPyprohly:mainfrom
Open
Fix KeyError when active_user_count missing from API response#2johntrandall wants to merge 1 commit intoPyprohly:mainfrom
johntrandall wants to merge 1 commit intoPyprohly:mainfrom
Conversation
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>
3b96dd0 to
092a3e2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
active_user_countin subreddit responsesd['active_user_count']which raisesKeyErrorwhen the field is absentd.get('active_user_count')which returnsNonefor missing keys, letting the existing ternary gracefully fall back to-1Details
In
redditwarp/models/subreddit.pyline 215, theSubredditData.__init__constructor accesses:When Reddit omits
active_user_countfrom the API payload, this raises aKeyError. The fix uses.get():This is consistent with the existing intent — the code already handles
Noneby defaulting to-1, so handling a missing key the same way is the natural extension.