Skip to content

Commit

Permalink
(#1164) Add types to listing/mixins/subreddit.py (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonCoderAS authored and bboe committed Dec 30, 2019
1 parent d358064 commit 6aa981e
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions praw/models/listing/mixins/subreddit.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,33 +10,9 @@
from .rising import RisingListingMixin


class SubredditListingMixin(
BaseListingMixin, GildedListingMixin, RisingListingMixin
):
"""Adds additional methods pertaining to Subreddit-like instances."""

@cachedproperty
def comments(self):
"""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, _data):
"""Initialize a SubredditListingMixin instance.
:param reddit: An instance of :class:`.Reddit`.
"""
super().__init__(reddit, _data=_data)
Comment = TypeVar("Comment")
Reddit = TypeVar("Reddit")
Subreddit = TypeVar("Subreddit")


class CommentHelper(PRAWBase):
Expand All @@ -45,12 +22,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
Expand All @@ -65,3 +44,32 @@ def __call__(self, **generator_kwargs):
"""
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)

0 comments on commit 6aa981e

Please sign in to comment.