From 2fc28b2cbafec9418f9205acf036aa0b2cbf5d1f Mon Sep 17 00:00:00 2001 From: priya-sundaram-dev Date: Thu, 3 Sep 2026 22:15:05 +0000 Subject: [PATCH 1/4] fix(reddit): remove unreachable 429 check after raise_for_status() --- web_programming/reddit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/reddit.py b/web_programming/reddit.py index 863b44f60b16..e8c7c38ccca7 100644 --- a/web_programming/reddit.py +++ b/web_programming/reddit.py @@ -40,9 +40,9 @@ def get_subreddit_data( headers={"User-agent": "A random string"}, timeout=10, ) + # raise_for_status() already raises httpx.HTTPStatusError for any 4xx/5xx + # response (including 429), so no extra status check is needed here. response.raise_for_status() - if response.status_code == 429: - raise httpx.HTTPError(response=response) data = response.json() if not wanted_data: From e21f4431d9dcc1de277428cd9ed38ef64acc69a7 Mon Sep 17 00:00:00 2001 From: priya-sundaram-dev Date: Fri, 4 Sep 2026 06:07:31 +0000 Subject: [PATCH 2/4] refactor(reddit): use fluent httpx.get().raise_for_status().json() chain --- web_programming/reddit.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/web_programming/reddit.py b/web_programming/reddit.py index e8c7c38ccca7..6e67d6f66cda 100644 --- a/web_programming/reddit.py +++ b/web_programming/reddit.py @@ -35,16 +35,17 @@ def get_subreddit_data( if invalid_search_terms := ", ".join(sorted(set(wanted_data) - valid_terms)): msg = f"Invalid search term: {invalid_search_terms}" raise ValueError(msg) - response = httpx.get( - f"https://www.reddit.com/r/{subreddit}/{age}.json?limit={limit}", - headers={"User-agent": "A random string"}, - timeout=10, - ) # raise_for_status() already raises httpx.HTTPStatusError for any 4xx/5xx # response (including 429), so no extra status check is needed here. - response.raise_for_status() - - data = response.json() + data = ( + httpx.get( + f"https://www.reddit.com/r/{subreddit}/{age}.json?limit={limit}", + headers={"User-agent": "A random string"}, + timeout=10, + ) + .raise_for_status() + .json() + ) if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(limit)} From 9f7de4aad4519f0d6d4015eba34e88f08a25566f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 4 Sep 2026 08:16:34 +0200 Subject: [PATCH 3/4] Refactor age parameter to use Literal type Updated the age parameter to use Literal for specific values. --- web_programming/reddit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/reddit.py b/web_programming/reddit.py index 6e67d6f66cda..e7da20161059 100644 --- a/web_programming/reddit.py +++ b/web_programming/reddit.py @@ -5,7 +5,7 @@ # ] # /// -from __future__ import annotations +from typing import Literal import httpx @@ -23,7 +23,7 @@ def get_subreddit_data( - subreddit: str, limit: int = 1, age: str = "new", wanted_data: list | None = None + subreddit: str, limit: int = 1, age: Literal["new", "top", "hot"] = "new", wanted_data: list | None = None ) -> dict: """ subreddit : Subreddit to query From c9cafabb9ec1da4184439f5abc62072b06927f70 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 06:16:48 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/reddit.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web_programming/reddit.py b/web_programming/reddit.py index e7da20161059..362a22bf613c 100644 --- a/web_programming/reddit.py +++ b/web_programming/reddit.py @@ -23,7 +23,10 @@ def get_subreddit_data( - subreddit: str, limit: int = 1, age: Literal["new", "top", "hot"] = "new", wanted_data: list | None = None + subreddit: str, + limit: int = 1, + age: Literal["new", "top", "hot"] = "new", + wanted_data: list | None = None, ) -> dict: """ subreddit : Subreddit to query