Skip to content

KeyError: 'active_user_count' when subreddit JSON omits key (use .get) #3

@nttylock

Description

@nttylock

Summary

Subreddit.__init__ uses d['active_user_count'] in redditwarp/models/subreddit.py (around line 215). Reddit sometimes omits this key entirely in JSON returned from GET /r/{subreddit}/about, which raises KeyError: 'active_user_count'. That is distinct from the key being present with a null value.

Expected behavior

If the key is missing, treat it like None and preserve the documented semantics: viewing_count == -1 (same as when the object comes from search / unknown).

Suggested fix

# before
self.viewing_count: int = -1 if (x := d['active_user_count']) is None else x

# after
self.viewing_count: int = -1 if (x := d.get('active_user_count')) is None else x

Repro

import redditwarp.SYNC
client = redditwarp.SYNC.Client()
client.p.subreddit.fetch_by_name("Python")  # may raise KeyError depending on API payload

Downstream consumers (e.g. Hawstein/mcp-server-reddit get_subreddit_info) surface this as a generic MCP error.

Environment

  • redditwarp 1.3.0 (PyPI)
  • Python 3.14 (any supported 3.8+)

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions