From 79a2a672f3f8cc9ce33c1d1147e498204d075f26 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Fri, 27 Dec 2019 18:02:36 -0500 Subject: [PATCH 1/5] (#1164) Add types to listing/mixins/redditor.py --- praw/models/listing/mixins/redditor.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/praw/models/listing/mixins/redditor.py b/praw/models/listing/mixins/redditor.py index d0bd4498b..d8db443e9 100644 --- a/praw/models/listing/mixins/redditor.py +++ b/praw/models/listing/mixins/redditor.py @@ -1,4 +1,5 @@ """Provide the RedditorListingMixin class.""" +from typing import Any, Dict, Generator, TypeVar, Union from urllib.parse import urljoin from ....util.cache import cachedproperty @@ -6,12 +7,15 @@ from .base import BaseListingMixin from .gilded import GildedListingMixin +Reddit = TypeVar("Reddit") +_SubListing = TypeVar("_SubListing") + 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 @@ -26,7 +30,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 @@ -40,7 +44,7 @@ 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 @@ -56,7 +60,7 @@ 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 @@ -74,7 +78,7 @@ 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 @@ -90,7 +94,7 @@ 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 @@ -106,7 +110,7 @@ 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 @@ -126,7 +130,7 @@ def upvoted(self, **generator_kwargs): class SubListing(BaseListingMixin): """Helper class for generating :class:`.ListingGenerator` objects.""" - def __init__(self, reddit, base_path, subpath): + def __init__(self, reddit: Reddit, base_path: str, subpath: str): """Initialize a SubListing instance. :param reddit: An instance of :class:`.Reddit`. From 952248cd021e7fec5ab3d8abafe41a09ffde988b Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Fri, 27 Dec 2019 18:03:10 -0500 Subject: [PATCH 2/5] Address black --- praw/models/listing/mixins/redditor.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/praw/models/listing/mixins/redditor.py b/praw/models/listing/mixins/redditor.py index d8db443e9..a65fd2769 100644 --- a/praw/models/listing/mixins/redditor.py +++ b/praw/models/listing/mixins/redditor.py @@ -44,7 +44,9 @@ def submissions(self) -> _SubListing: """ return SubListing(self._reddit, self._path, "submitted") - def downvoted(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generator[Any, None, None]: + 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 @@ -60,7 +62,9 @@ def downvoted(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Gene self._reddit, urljoin(self._path, "downvoted"), **generator_kwargs ) - def gildings(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generator[Any, None, None]: + 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 @@ -78,7 +82,9 @@ def gildings(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Gener **generator_kwargs ) - def hidden(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generator[Any, None, None]: + 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 @@ -94,7 +100,9 @@ def hidden(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generat self._reddit, urljoin(self._path, "hidden"), **generator_kwargs ) - def saved(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generator[Any, None, None]: + 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 @@ -110,7 +118,9 @@ def saved(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generato self._reddit, urljoin(self._path, "saved"), **generator_kwargs ) - def upvoted(self, **generator_kwargs: Union[str, int, Dict[str, str]]) -> Generator[Any, None, None]: + 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 From 95da99372ea20de83af3e3030791270a05869345 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 11:49:58 -0500 Subject: [PATCH 3/5] Sort TypeVars --- praw/models/listing/mixins/redditor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/praw/models/listing/mixins/redditor.py b/praw/models/listing/mixins/redditor.py index a65fd2769..e1b017fe3 100644 --- a/praw/models/listing/mixins/redditor.py +++ b/praw/models/listing/mixins/redditor.py @@ -7,8 +7,8 @@ from .base import BaseListingMixin from .gilded import GildedListingMixin -Reddit = TypeVar("Reddit") _SubListing = TypeVar("_SubListing") +Reddit = TypeVar("Reddit") class RedditorListingMixin(BaseListingMixin, GildedListingMixin): From 53226f8487cbce428789ecebc3000c7047a2f742 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 12:28:48 -0500 Subject: [PATCH 4/5] Re-order --- praw/models/listing/mixins/redditor.py | 39 +++++++++++++------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/praw/models/listing/mixins/redditor.py b/praw/models/listing/mixins/redditor.py index e1b017fe3..f3952ebed 100644 --- a/praw/models/listing/mixins/redditor.py +++ b/praw/models/listing/mixins/redditor.py @@ -7,15 +7,31 @@ from .base import BaseListingMixin from .gilded import GildedListingMixin -_SubListing = TypeVar("_SubListing") 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) -> _SubListing: + 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 @@ -30,7 +46,7 @@ def comments(self) -> _SubListing: return SubListing(self._reddit, self._path, "comments") @cachedproperty - def submissions(self) -> _SubListing: + 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 @@ -135,20 +151,3 @@ def upvoted( return ListingGenerator( self._reddit, urljoin(self._path, "upvoted"), **generator_kwargs ) - - -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) From ef213324798c8dd373fe5b8abe8ab2744bbed204 Mon Sep 17 00:00:00 2001 From: PokestarFan Date: Sun, 29 Dec 2019 12:29:00 -0500 Subject: [PATCH 5/5] Black format --- praw/models/listing/mixins/redditor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/praw/models/listing/mixins/redditor.py b/praw/models/listing/mixins/redditor.py index f3952ebed..10770ff40 100644 --- a/praw/models/listing/mixins/redditor.py +++ b/praw/models/listing/mixins/redditor.py @@ -9,6 +9,7 @@ Reddit = TypeVar("Reddit") + class SubListing(BaseListingMixin): """Helper class for generating :class:`.ListingGenerator` objects.""" @@ -26,7 +27,6 @@ def __init__(self, reddit: Reddit, base_path: str, subpath: str): self._path = urljoin(base_path, subpath) - class RedditorListingMixin(BaseListingMixin, GildedListingMixin): """Adds additional methods pertaining to Redditor instances."""