Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1164) Add types to listing/mixins/redditor.py #1206

Merged
merged 5 commits into from
Dec 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions praw/models/listing/mixins/redditor.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
"""Provide the RedditorListingMixin class."""
from typing import Any, Dict, Generator, TypeVar, Union
from urllib.parse import urljoin

from ....util.cache import cachedproperty
from ..generator import ListingGenerator
from .base import BaseListingMixin
from .gilded import GildedListingMixin

Reddit = TypeVar("Reddit")


class SubListing(BaseListingMixin):
"""Helper class for generating :class:`.ListingGenerator` objects."""

def __init__(self, reddit: Reddit, base_path: str, subpath: str):
"""Initialize a SubListing instance.

:param reddit: An instance of :class:`.Reddit`.
:param base_path: The path to the object up to this point.
:param subpath: The additional path to this sublisting.

"""
super().__init__(reddit, _data=None)
self._listing_use_sort = True
self._reddit = reddit
self._path = urljoin(base_path, subpath)


class RedditorListingMixin(BaseListingMixin, GildedListingMixin):
"""Adds additional methods pertaining to Redditor instances."""

@cachedproperty
def comments(self):
def comments(self) -> SubListing:
r"""Provide an instance of :class:`.SubListing` for comment access.

For example, to output the first line of all new comments by
Expand All @@ -26,7 +46,7 @@ def comments(self):
return SubListing(self._reddit, self._path, "comments")

@cachedproperty
def submissions(self):
def submissions(self) -> SubListing:
"""Provide an instance of :class:`.SubListing` for submission access.

For example, to output the title's of top 100 of all time submissions
Expand All @@ -40,7 +60,9 @@ def submissions(self):
"""
return SubListing(self._reddit, self._path, "submitted")

def downvoted(self, **generator_kwargs):
def downvoted(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Generator[Any, None, None]:
"""Return a :class:`.ListingGenerator` for items the user has downvoted.

May raise ``prawcore.Forbidden`` after issuing the request if the user
Expand All @@ -56,7 +78,9 @@ def downvoted(self, **generator_kwargs):
self._reddit, urljoin(self._path, "downvoted"), **generator_kwargs
)

def gildings(self, **generator_kwargs):
def gildings(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Generator[Any, None, None]:
"""Return a :class:`.ListingGenerator` for items the user has gilded.

May raise ``prawcore.Forbidden`` after issuing the request if the user
Expand All @@ -74,7 +98,9 @@ def gildings(self, **generator_kwargs):
**generator_kwargs
)

def hidden(self, **generator_kwargs):
def hidden(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Generator[Any, None, None]:
"""Return a :class:`.ListingGenerator` for items the user has hidden.

May raise ``prawcore.Forbidden`` after issuing the request if the user
Expand All @@ -90,7 +116,9 @@ def hidden(self, **generator_kwargs):
self._reddit, urljoin(self._path, "hidden"), **generator_kwargs
)

def saved(self, **generator_kwargs):
def saved(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Generator[Any, None, None]:
"""Return a :class:`.ListingGenerator` for items the user has saved.

May raise ``prawcore.Forbidden`` after issuing the request if the user
Expand All @@ -106,7 +134,9 @@ def saved(self, **generator_kwargs):
self._reddit, urljoin(self._path, "saved"), **generator_kwargs
)

def upvoted(self, **generator_kwargs):
def upvoted(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Generator[Any, None, None]:
"""Return a :class:`.ListingGenerator` for items the user has upvoted.

May raise ``prawcore.Forbidden`` after issuing the request if the user
Expand All @@ -121,20 +151,3 @@ def upvoted(self, **generator_kwargs):
return ListingGenerator(
self._reddit, urljoin(self._path, "upvoted"), **generator_kwargs
)


class SubListing(BaseListingMixin):
"""Helper class for generating :class:`.ListingGenerator` objects."""

def __init__(self, reddit, base_path, subpath):
"""Initialize a SubListing instance.

:param reddit: An instance of :class:`.Reddit`.
:param base_path: The path to the object up to this point.
:param subpath: The additional path to this sublisting.

"""
super().__init__(reddit, _data=None)
self._listing_use_sort = True
self._reddit = reddit
self._path = urljoin(base_path, subpath)