From 9472aac542d20c95c6246447b4a48a9dd32e603a Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Fri, 27 Dec 2019 18:21:24 -0500 Subject: [PATCH 1/4] (#1164) Add types to listing/mixins/subreddit.py --- praw/models/listing/mixins/subreddit.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/praw/models/listing/mixins/subreddit.py b/praw/models/listing/mixins/subreddit.py index a9c78580f..a569addb9 100644 --- a/praw/models/listing/mixins/subreddit.py +++ b/praw/models/listing/mixins/subreddit.py @@ -1,4 +1,5 @@ """Provide the SubredditListingMixin class.""" +from typing import Any, Dict, Generator, TypeVar, Union from urllib.parse import urljoin from ....util.cache import cachedproperty @@ -8,6 +9,11 @@ from .gilded import GildedListingMixin from .rising import RisingListingMixin +Comment = TypeVar("Comment") +_CommentHelper = TypeVar("_CommentHelper") +Reddit = TypeVar("Reddit") +Subreddit = TypeVar("Subreddit") + class SubredditListingMixin( BaseListingMixin, GildedListingMixin, RisingListingMixin @@ -15,7 +21,7 @@ class SubredditListingMixin( """Adds additional methods pertaining to Subreddit-like instances.""" @cachedproperty - def comments(self): + def comments(self) -> _CommentHelper: """Provide an instance of :class:`.CommentHelper`. For example, to output the author of the 25 most recent comments of @@ -29,7 +35,7 @@ def comments(self): """ return CommentHelper(self) - def __init__(self, reddit, _data): + def __init__(self, reddit: Reddit, _data: Dict[str, Any]): """Initialize a SubredditListingMixin instance. :param reddit: An instance of :class:`.Reddit`. @@ -45,12 +51,14 @@ class CommentHelper(PRAWBase): def _path(self): return urljoin(self.subreddit._path, "comments/") - def __init__(self, subreddit): + def __init__(self, subreddit: Subreddit): """Initialize a CommentHelper instance.""" super().__init__(subreddit._reddit, _data=None) self.subreddit = subreddit - def __call__(self, **generator_kwargs): + def __call__( + self, **generator_kwargs: Union[str, int, Dict[str, str]] + ) -> Generator[Any, None, None]: """Return a :class:`.ListingGenerator` for the Subreddit's comments. Additional keyword arguments are passed in the initialization of From 2e7f3a3e833d3a5c7311b1c981f8fd54a806ea03 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 11:49:03 -0500 Subject: [PATCH 2/4] Sort TypeVars --- praw/models/listing/mixins/subreddit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/praw/models/listing/mixins/subreddit.py b/praw/models/listing/mixins/subreddit.py index a569addb9..408bdb8e3 100644 --- a/praw/models/listing/mixins/subreddit.py +++ b/praw/models/listing/mixins/subreddit.py @@ -9,8 +9,8 @@ from .gilded import GildedListingMixin from .rising import RisingListingMixin -Comment = TypeVar("Comment") _CommentHelper = TypeVar("_CommentHelper") +Comment = TypeVar("Comment") Reddit = TypeVar("Reddit") Subreddit = TypeVar("Subreddit") From b99084a725ab790c3c6cde26b561df10db7e971f Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 13:19:28 -0500 Subject: [PATCH 3/4] Move around --- praw/models/listing/mixins/subreddit.py | 62 ++++++++++++------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/praw/models/listing/mixins/subreddit.py b/praw/models/listing/mixins/subreddit.py index 408bdb8e3..59d8b9aca 100644 --- a/praw/models/listing/mixins/subreddit.py +++ b/praw/models/listing/mixins/subreddit.py @@ -9,41 +9,11 @@ from .gilded import GildedListingMixin from .rising import RisingListingMixin -_CommentHelper = TypeVar("_CommentHelper") + Comment = TypeVar("Comment") Reddit = TypeVar("Reddit") Subreddit = TypeVar("Subreddit") - -class SubredditListingMixin( - BaseListingMixin, GildedListingMixin, RisingListingMixin -): - """Adds additional methods pertaining to Subreddit-like instances.""" - - @cachedproperty - def comments(self) -> _CommentHelper: - """Provide an instance of :class:`.CommentHelper`. - - For example, to output the author of the 25 most recent comments of - ``/r/redditdev`` execute: - - .. code:: python - - for comment in reddit.subreddit('redditdev').comments(limit=25): - print(comment.author) - - """ - return CommentHelper(self) - - def __init__(self, reddit: Reddit, _data: Dict[str, Any]): - """Initialize a SubredditListingMixin instance. - - :param reddit: An instance of :class:`.Reddit`. - - """ - super().__init__(reddit, _data=_data) - - class CommentHelper(PRAWBase): """Provide a set of functions to interact with a subreddit's comments.""" @@ -73,3 +43,33 @@ def __call__( """ return ListingGenerator(self._reddit, self._path, **generator_kwargs) + + + +class SubredditListingMixin( + BaseListingMixin, GildedListingMixin, RisingListingMixin +): + """Adds additional methods pertaining to Subreddit-like instances.""" + + @cachedproperty + def comments(self) -> CommentHelper: + """Provide an instance of :class:`.CommentHelper`. + + For example, to output the author of the 25 most recent comments of + ``/r/redditdev`` execute: + + .. code:: python + + for comment in reddit.subreddit('redditdev').comments(limit=25): + print(comment.author) + + """ + return CommentHelper(self) + + def __init__(self, reddit: Reddit, _data: Dict[str, Any]): + """Initialize a SubredditListingMixin instance. + + :param reddit: An instance of :class:`.Reddit`. + + """ + super().__init__(reddit, _data=_data) \ No newline at end of file From 04f12ae793a23266d4da2153afcd2dd16b519769 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 13:19:41 -0500 Subject: [PATCH 4/4] Black format --- praw/models/listing/mixins/subreddit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/praw/models/listing/mixins/subreddit.py b/praw/models/listing/mixins/subreddit.py index 59d8b9aca..8a5da2fe9 100644 --- a/praw/models/listing/mixins/subreddit.py +++ b/praw/models/listing/mixins/subreddit.py @@ -14,6 +14,7 @@ Reddit = TypeVar("Reddit") Subreddit = TypeVar("Subreddit") + class CommentHelper(PRAWBase): """Provide a set of functions to interact with a subreddit's comments.""" @@ -45,7 +46,6 @@ def __call__( return ListingGenerator(self._reddit, self._path, **generator_kwargs) - class SubredditListingMixin( BaseListingMixin, GildedListingMixin, RisingListingMixin ): @@ -72,4 +72,4 @@ def __init__(self, reddit: Reddit, _data: Dict[str, Any]): :param reddit: An instance of :class:`.Reddit`. """ - super().__init__(reddit, _data=_data) \ No newline at end of file + super().__init__(reddit, _data=_data)