Skip to content

Commit da15fec

Browse files
Switch sort enums to StrEnum
1 parent f39989a commit da15fec

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/redditpythonapi/reddit.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Using a wrapper simplifies accessing the API, mostly due to handling OAuth.
44
"""
55

6-
from enum import Enum, auto
6+
from enum import StrEnum
77
from logging import getLogger
88
from time import time_ns
99
from typing import Any
@@ -13,25 +13,25 @@
1313
Article = dict[str, Any]
1414

1515

16-
class ArticlesSortType(Enum):
16+
class ArticlesSortType(StrEnum):
1717
"""Enum with all viable sorting types"""
1818

19-
HOT = auto()
20-
NEW = auto()
21-
RISING = auto()
22-
TOP = auto()
23-
CONTROVERSIAL = auto()
19+
HOT = "hot"
20+
NEW = "new"
21+
RISING = "rising"
22+
TOP = "top"
23+
CONTROVERSIAL = "controversial"
2424

2525

26-
class ArticlesSortTime(Enum):
26+
class ArticlesSortTime(StrEnum):
2727
"""Enum with all viable sort times"""
2828

29-
HOUR = auto()
30-
DAY = auto()
31-
WEEK = auto()
32-
MONTH = auto()
33-
YEAR = auto()
34-
ALL = auto()
29+
HOUR = "hour"
30+
DAY = "day"
31+
WEEK = "week"
32+
MONTH = "month"
33+
YEAR = "year"
34+
ALL = "all"
3535

3636

3737
class Reddit:
@@ -139,14 +139,11 @@ def _prepare_params(
139139
time: ArticlesSortTime | None = None,
140140
limit: int | None = None,
141141
):
142-
params = dict()
143-
if sort is not None:
144-
params["sort"] = sort.name.lower()
145-
if time is not None:
146-
params["t"] = time.name.lower()
147-
if limit is not None:
148-
params["limit"] = limit
149-
return params
142+
return {
143+
**({"sort": sort.value} if sort is not None else {}),
144+
**({"t": time.value} if time is not None else {}),
145+
**({"limit": limit} if limit is not None else {}),
146+
}
150147

151148
async def _get_articles(self, url: str, params: dict[str, Any]) -> list[Article]:
152149
if self._access_token_expires_in <= time_ns():

0 commit comments

Comments
 (0)