diff --git a/CHANGES.rst b/CHANGES.rst index 4bcbfec1..26b29459 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -21,6 +21,11 @@ Unreleased - An import error when using Async PRAW in environments where ``libsqlite3-dev`` is needed to utilize ``aiosqlite`` package which depends on the ``sqlite3`` builtin. +**Deprecated** + +- The keyword argument ``lazy`` has been replace by ``fetch`` to consolidate the keyword + argument used to explicitly perform a fetch when initializing an object. + 7.4.0 (2021/07/30) ------------------ diff --git a/README.rst b/README.rst index 6f2e9c82..18176b44 100644 --- a/README.rst +++ b/README.rst @@ -81,7 +81,7 @@ With the ``reddit`` instance you can then interact with Reddit: # Comment on a known submission submission = await reddit.submission( - url="https://www.reddit.com/comments/5e1az9", lazy=True + url="https://www.reddit.com/comments/5e1az9", fetch=False ) await submission.reply("Super rad!") diff --git a/asyncpraw/models/comment_forest.py b/asyncpraw/models/comment_forest.py index 4f926a6d..f9cd40ed 100644 --- a/asyncpraw/models/comment_forest.py +++ b/asyncpraw/models/comment_forest.py @@ -153,7 +153,7 @@ async def replace_more( .. code-block:: python - submission = await reddit.submission("3hahrw", lazy=True) + submission = await reddit.submission("3hahrw", fetch=False) comments = await submission.comments() await comments.replace_more() diff --git a/asyncpraw/models/helpers.py b/asyncpraw/models/helpers.py index abcb5248..78c07532 100644 --- a/asyncpraw/models/helpers.py +++ b/asyncpraw/models/helpers.py @@ -34,7 +34,7 @@ async def __call__( await livethread.close() :param id: A live thread ID, e.g., ``ukaeu1ik4sw5``. - :param fetch: Determines if the object is lazily loaded (default: False). + :param fetch: Determines if Async PRAW will fetch the object (default: False). """ live_thread = LiveThread(self._reddit, id=id) @@ -154,7 +154,7 @@ async def __call__( :param redditor: A redditor name (e.g., ``"spez"``) or :class:`~.Redditor` instance who owns the multireddit. :param name: The name of the multireddit. - :param fetch: Determines if the object is lazily loaded (default: False). + :param fetch: Determines if Async PRAW will fetch the object (default: False). """ path = f"/user/{redditor}/m/{name}" @@ -227,7 +227,7 @@ async def __call__( print(comment.author) :param display_name: The name of the subreddit. - :param fetch: Determines if the object is lazily loaded (default: False). + :param fetch: Determines if Async PRAW will fetch the object (default: False). """ lower_name = display_name.lower() diff --git a/asyncpraw/models/listing/mixins/submission.py b/asyncpraw/models/listing/mixins/submission.py index 02d28f3d..ba08d664 100644 --- a/asyncpraw/models/listing/mixins/submission.py +++ b/asyncpraw/models/listing/mixins/submission.py @@ -24,7 +24,7 @@ def duplicates( .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) async for duplicate in submission.duplicates(): # process each duplicate diff --git a/asyncpraw/models/reddit/collections.py b/asyncpraw/models/reddit/collections.py index f8c2ec6b..6055efeb 100644 --- a/asyncpraw/models/reddit/collections.py +++ b/asyncpraw/models/reddit/collections.py @@ -5,6 +5,7 @@ from ...exceptions import ClientException from ...util.cache import cachedproperty from ..base import AsyncPRAWBase +from ..util import deprecate_lazy from .base import RedditBase from .redditor import Redditor from .submission import Submission @@ -505,17 +506,19 @@ def mod(self) -> SubredditCollectionsModeration: """ return SubredditCollectionsModeration(self._reddit, self.subreddit) + @deprecate_lazy async def __call__( self, collection_id: Optional[str] = None, permalink: Optional[str] = None, - lazy: bool = False, + fetch: bool = True, + **kwargs, ): """Return the :class:`.Collection` with the specified ID. :param collection_id: The ID of a Collection (default: None). :param permalink: The permalink of a Collection (default: None). - :param lazy: If True, object is loaded lazily (default: False) + :param fetch: Determines if Async PRAW will fetch the object (default: True). :returns: The specified Collection. @@ -543,7 +546,7 @@ async def __call__( .. code-block:: python subreddit = await reddit.subreddit("SUBREDDIT", fetch=True) - collection = await subreddit.collections(uuid, lazy=True) + collection = await subreddit.collections(uuid, fetch=False) await collection.mod.add("submission_id") """ @@ -554,7 +557,7 @@ async def __call__( collection = Collection( self._reddit, collection_id=collection_id, permalink=permalink ) - if not lazy: + if fetch: await collection._fetch() return collection diff --git a/asyncpraw/models/reddit/comment.py b/asyncpraw/models/reddit/comment.py index 7b235f03..1f81e5ee 100644 --- a/asyncpraw/models/reddit/comment.py +++ b/asyncpraw/models/reddit/comment.py @@ -101,7 +101,7 @@ def mod(self) -> "asyncpraw.models.reddit.comment.CommentModeration": .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.approve() """ @@ -242,7 +242,7 @@ async def parent( .. code-block:: python - comment = await reddit.comment("cklhv0f", lazy=True) + comment = await reddit.comment("cklhv0f", fetch=False) parent = await comment.parent() # `replies` is empty until the comment is refreshed print(parent.replies) # Output: [] @@ -302,7 +302,7 @@ async def refresh(self): .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.refresh() """ @@ -352,7 +352,7 @@ class CommentModeration(ThingModerationMixin): .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.approve() """ @@ -375,7 +375,7 @@ async def show(self): .. code-block:: python # Uncollapse a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.show() """ diff --git a/asyncpraw/models/reddit/emoji.py b/asyncpraw/models/reddit/emoji.py index cc2f6948..e25a6167 100644 --- a/asyncpraw/models/reddit/emoji.py +++ b/asyncpraw/models/reddit/emoji.py @@ -4,6 +4,7 @@ from ...const import API_PATH from ...exceptions import ClientException +from ..util import deprecate_lazy from .base import RedditBase if TYPE_CHECKING: # pragma: no cover @@ -139,11 +140,12 @@ async def update( class SubredditEmoji: """Provides a set of functions to a Subreddit for emoji.""" - async def get_emoji(self, name: str, lazy: bool = False) -> Emoji: + @deprecate_lazy + async def get_emoji(self, name: str, fetch: bool = True, **kwargs) -> Emoji: """Return the Emoji for the subreddit named ``name``. - :param name: The name of the emoji - :param lazy: If True, object is loaded lazily (default: False) + :param name: The name of the emoji. + :param fetch: Determines if Async PRAW will fetch the object (default: True). This method is to be used to fetch a specific emoji url, like so: @@ -159,12 +161,12 @@ async def get_emoji(self, name: str, lazy: bool = False) -> Emoji: .. code-block:: python subreddit = await reddit.subreddit("praw_test") - emoji = await subreddit.emoji.get_emoji("test", lazy=True) + emoji = await subreddit.emoji.get_emoji("test", fetch=False) await emoji.delete() """ emoji = Emoji(self._reddit, self.subreddit, name) - if not lazy: + if fetch: await emoji._fetch() return emoji diff --git a/asyncpraw/models/reddit/live.py b/asyncpraw/models/reddit/live.py index 836848a7..cfeea695 100644 --- a/asyncpraw/models/reddit/live.py +++ b/asyncpraw/models/reddit/live.py @@ -5,7 +5,7 @@ from ...util.cache import cachedproperty from ..list.redditor import RedditorList from ..listing.generator import ListingGenerator -from ..util import stream_generator +from ..util import deprecate_lazy, stream_generator from .base import RedditBase from .mixins import FullnameMixin from .redditor import Redditor @@ -96,7 +96,7 @@ async def invite( .. code-block:: python thread = await reddit.live("ukaeu1ik4sw5") - redditor = await reddit.redditor("spez", lazy=True) + redditor = await reddit.redditor("spez", fetch=False) # "manage" and "settings" permissions await thread.contributor.invite(redditor, ["manage", "settings"]) @@ -140,7 +140,7 @@ async def remove(self, redditor: Union[str, "asyncpraw.models.Redditor"]): .. code-block:: python thread = await reddit.live("ukaeu1ik4sw5") - redditor = await reddit.redditor("spez", lazy=True) + redditor = await reddit.redditor("spez", fetch=False) await thread.contributor.remove(redditor) await thread.contributor.remove("t2_1w72") # with fullname @@ -164,7 +164,7 @@ async def remove_invite(self, redditor: Union[str, "asyncpraw.models.Redditor"]) .. code-block:: python thread = await reddit.live("ukaeu1ik4sw5") - redditor = await reddit.redditor("spez", lazy=True) + redditor = await reddit.redditor("spez", fetch=False) await thread.contributor.remove_invite(redditor) await thread.contributor.remove_invite("t2_1w72") # with fullname @@ -365,14 +365,15 @@ def __eq__(self, other: Union[str, "asyncpraw.models.LiveThread"]) -> bool: return other == str(self) return isinstance(other, self.__class__) and str(self) == str(other) + @deprecate_lazy async def get_update( - self, update_id: str, lazy: bool = False + self, update_id: str, fetch: bool = True, **kwargs ) -> "asyncpraw.models.LiveUpdate": """Return a :class:`.LiveUpdate` instance. :param update_id: A live update ID, e.g., ``"7827987a-c998-11e4-a0b9-22000b6a88d2"``. - :param lazy: If True, object is loaded lazily (default: False). + :param fetch: Determines if Async PRAW will fetch the object (default: True). Usage: @@ -390,12 +391,12 @@ async def get_update( .. code-block:: python thread = await reddit.live("ukaeu1ik4sw5") - update = await thread.get_update("7827987a-c998-11e4-a0b9-22000b6a88d2", lazy=True) + update = await thread.get_update("7827987a-c998-11e4-a0b9-22000b6a88d2", fetch=False) update.contrib # LiveUpdateContribution instance """ update = LiveUpdate(self._reddit, self.id, update_id) - if not lazy: + if fetch: await update._fetch() return update @@ -717,7 +718,7 @@ def contrib(self) -> "asyncpraw.models.reddit.live.LiveUpdateContribution": .. code-block:: python thread = await reddit.live("ukaeu1ik4sw5") - update = await thread.get_update("7827987a-c998-11e4-a0b9-22000b6a88d2", lazy=True) + update = await thread.get_update("7827987a-c998-11e4-a0b9-22000b6a88d2", fetch=False) update.contrib # LiveUpdateContribution instance """ diff --git a/asyncpraw/models/reddit/mixins/__init__.py b/asyncpraw/models/reddit/mixins/__init__.py index 58ca764b..4bde10d0 100644 --- a/asyncpraw/models/reddit/mixins/__init__.py +++ b/asyncpraw/models/reddit/mixins/__init__.py @@ -55,10 +55,10 @@ async def approve(self): .. code-block:: python # approve a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.approve() # approve a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.approve() """ @@ -81,10 +81,10 @@ async def distinguish(self, how="yes", sticky=False): .. code-block:: python # distinguish and sticky a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.distinguish(how="yes", sticky=True) # undistinguish a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.distinguish(how="no") .. seealso:: @@ -110,10 +110,10 @@ async def ignore_reports(self): .. code-block:: python # ignore future reports on a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.ignore_reports() # ignore future reports on a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.ignore_reports() .. seealso:: @@ -133,10 +133,10 @@ async def lock(self): .. code-block:: python # lock a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.lock() # lock a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.lock() .. seealso:: @@ -164,15 +164,15 @@ async def remove(self, spam=False, mod_note="", reason_id=None): .. code-block:: python # remove a comment and mark as spam: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.remove(spam=True) # remove a submission - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.remove() # remove a submission with a removal reason sub = await reddit.subreddit("subreddit") reason = await sub.mod.removal_reasons.get_reason("110ni21zo23ql") - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.remove(reason_id=reason.id) """ @@ -234,10 +234,10 @@ async def undistinguish(self): .. code-block:: python # undistinguish a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.undistinguish() # undistinguish a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.undistinguish() .. seealso:: @@ -258,10 +258,10 @@ async def unignore_reports(self): .. code-block:: python # accept future reports on a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.unignore_reports() # accept future reports on a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.unignore_reports() .. seealso:: @@ -281,10 +281,10 @@ async def unlock(self): .. code-block:: python # unlock a comment: - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.mod.unlock() # unlock a submission: - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.unlock() .. seealso:: diff --git a/asyncpraw/models/reddit/mixins/editable.py b/asyncpraw/models/reddit/mixins/editable.py index 3854eefd..3855da45 100644 --- a/asyncpraw/models/reddit/mixins/editable.py +++ b/asyncpraw/models/reddit/mixins/editable.py @@ -12,10 +12,10 @@ async def delete(self): .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.delete() - submission = await reddit.submission("8dmv8z", lazy=True) + submission = await reddit.submission("8dmv8z", fetch=False) await submission.delete() """ diff --git a/asyncpraw/models/reddit/mixins/inboxtoggleable.py b/asyncpraw/models/reddit/mixins/inboxtoggleable.py index bbf67318..5d5c0bb1 100644 --- a/asyncpraw/models/reddit/mixins/inboxtoggleable.py +++ b/asyncpraw/models/reddit/mixins/inboxtoggleable.py @@ -12,10 +12,10 @@ async def disable_inbox_replies(self): .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.disable_inbox_replies() - submission = await reddit.submission("8dmv8z", lazy=True) + submission = await reddit.submission("8dmv8z", fetch=False) await submission.disable_inbox_replies() .. seealso:: @@ -34,10 +34,10 @@ async def enable_inbox_replies(self): .. code-block:: python - comment = await reddit.comment("dkk4qjd", lazy=True) + comment = await reddit.comment("dkk4qjd", fetch=False) await comment.enable_inbox_replies() - submission = await reddit.submission("8dmv8z", lazy=True) + submission = await reddit.submission("8dmv8z", fetch=False) await submission.enable_inbox_replies() .. seealso:: diff --git a/asyncpraw/models/reddit/mixins/messageable.py b/asyncpraw/models/reddit/mixins/messageable.py index 908a7c16..86d164b9 100644 --- a/asyncpraw/models/reddit/mixins/messageable.py +++ b/asyncpraw/models/reddit/mixins/messageable.py @@ -34,14 +34,14 @@ async def message( .. code-block:: python - redditor = await reddit.redditor("spez", lazy=True) + redditor = await reddit.redditor("spez", fetch=False) await redditor.message("TEST", "test message from Async PRAW") To send a message to ``u/spez`` from the moderators of ``r/test`` try: .. code-block:: python - redditor = await reddit.redditor("spez", lazy=True) + redditor = await reddit.redditor("spez", fetch=False) await redditor.message("TEST", "test message from r/test", from_subreddit="test") To send a message to the moderators of ``r/test``, try: diff --git a/asyncpraw/models/reddit/mixins/replyable.py b/asyncpraw/models/reddit/mixins/replyable.py index 5a7f2645..5de18dd0 100644 --- a/asyncpraw/models/reddit/mixins/replyable.py +++ b/asyncpraw/models/reddit/mixins/replyable.py @@ -29,10 +29,10 @@ async def reply(self, body: str): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.reply("reply") - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.reply("reply") """ diff --git a/asyncpraw/models/reddit/mixins/reportable.py b/asyncpraw/models/reddit/mixins/reportable.py index e70f92ed..bb2eb479 100644 --- a/asyncpraw/models/reddit/mixins/reportable.py +++ b/asyncpraw/models/reddit/mixins/reportable.py @@ -17,10 +17,10 @@ async def report(self, reason: str): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.report("report reason") - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.report("report reason") """ diff --git a/asyncpraw/models/reddit/mixins/savable.py b/asyncpraw/models/reddit/mixins/savable.py index d55ba61b..a1ed5b56 100644 --- a/asyncpraw/models/reddit/mixins/savable.py +++ b/asyncpraw/models/reddit/mixins/savable.py @@ -17,10 +17,10 @@ async def save(self, category: Optional[str] = None): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.save(category="view later") - comment = await reddit.comment(id="dxolpyc", lazy=True, lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.save() .. seealso:: @@ -39,10 +39,10 @@ async def unsave(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.unsave() - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.unsave() .. seealso:: diff --git a/asyncpraw/models/reddit/mixins/votable.py b/asyncpraw/models/reddit/mixins/votable.py index 59b5a35f..b1799c63 100644 --- a/asyncpraw/models/reddit/mixins/votable.py +++ b/asyncpraw/models/reddit/mixins/votable.py @@ -25,10 +25,10 @@ async def clear_vote(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.clear_vote() - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.clear_vote() """ @@ -49,10 +49,10 @@ async def downvote(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.downvote() - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.downvote() .. seealso:: @@ -77,10 +77,10 @@ async def upvote(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.upvote() - comment = await reddit.comment(id="dxolpyc", lazy=True) + comment = await reddit.comment(id="dxolpyc", fetch=False) await comment.upvote() .. seealso:: diff --git a/asyncpraw/models/reddit/removal_reasons.py b/asyncpraw/models/reddit/removal_reasons.py index 81798f3d..71a3c5a9 100644 --- a/asyncpraw/models/reddit/removal_reasons.py +++ b/asyncpraw/models/reddit/removal_reasons.py @@ -4,6 +4,7 @@ from ...const import API_PATH from ...exceptions import ClientException +from ..util import deprecate_lazy from .base import RedditBase if TYPE_CHECKING: # pragma: no cover @@ -144,13 +145,14 @@ async def update(self, message: Optional[str] = None, title: Optional[str] = Non class SubredditRemovalReasons: """Provide a set of functions to a Subreddit's removal reasons.""" + @deprecate_lazy async def get_reason( - self, reason_id: Union[str, int, slice], lazy: bool = False + self, reason_id: Union[str, int, slice], fetch: bool = True, **kwargs ) -> RemovalReason: """Return the Removal Reason with the ID/number/slice ``reason_id``. - :param reason_id: The ID or index of the removal reason - :param lazy: If True, object is loaded lazily (default: False). + :param reason_id: The ID or index of the removal reason. + :param fetch: Determines if Async PRAW will fetch the object (default: True). This method is to be used to fetch a specific removal reason, like so: @@ -195,7 +197,7 @@ async def get_reason( reason_id = "141vv5c16py7d" subreddit = await reddit.subreddit("NAME") - reason = await subreddit.mod.removal_reasons.get_reason(reason_id, lazy=True) + reason = await subreddit.mod.removal_reasons.get_reason(reason_id, fetch=False) await reason.delete() """ @@ -204,7 +206,7 @@ async def get_reason( return reasons[reason_id] else: reason = RemovalReason(self._reddit, self.subreddit, reason_id) - if not lazy: + if fetch: await reason._fetch() return reason diff --git a/asyncpraw/models/reddit/submission.py b/asyncpraw/models/reddit/submission.py index 2443e371..739a6157 100644 --- a/asyncpraw/models/reddit/submission.py +++ b/asyncpraw/models/reddit/submission.py @@ -88,7 +88,7 @@ class SubmissionModeration(ThingModerationMixin): .. code-block:: python - submission = await reddit.submission(id="8dmv8z", lazy=True) + submission = await reddit.submission(id="8dmv8z", fetch=False) await submission.mod.approve() """ @@ -121,7 +121,7 @@ async def contest_mode(self, state: bool = True): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.contest_mode(state=True) """ @@ -149,7 +149,7 @@ async def flair( .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.flair(text="PRAW", css_class="bot") """ @@ -228,7 +228,7 @@ async def sfw(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.sfw() .. seealso:: @@ -250,7 +250,7 @@ async def spoiler(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.spoiler() .. seealso:: @@ -283,7 +283,7 @@ async def sticky(self, state: bool = True, bottom: bool = True): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mod.sticky() """ @@ -564,7 +564,7 @@ def mod(self) -> SubmissionModeration: .. code-block:: python - submission = await reddit.submission(id="8dmv8z", lazy=True) + submission = await reddit.submission(id="8dmv8z", fetch=False) await submission.mod.approve() """ @@ -678,7 +678,7 @@ async def mark_visited(self): .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.mark_visited() """ @@ -697,7 +697,7 @@ async def hide( .. code-block:: python - submission = await reddit.submission(id="5or86n", lazy=True) + submission = await reddit.submission(id="5or86n", fetch=False) await submission.hide() .. seealso:: diff --git a/asyncpraw/models/reddit/subreddit.py b/asyncpraw/models/reddit/subreddit.py index 0c0751f2..f5d9fd72 100644 --- a/asyncpraw/models/reddit/subreddit.py +++ b/asyncpraw/models/reddit/subreddit.py @@ -39,7 +39,7 @@ from ...util.cache import cachedproperty from ..listing.generator import ListingGenerator from ..listing.mixins import SubredditListingMixin -from ..util import permissions_string, stream_generator +from ..util import deprecate_lazy, permissions_string, stream_generator from .base import RedditBase from .emoji import SubredditEmoji from .mixins import FullnameMixin, MessageableMixin @@ -3345,7 +3345,7 @@ async def __call__( :param id: A reddit base36 conversation ID, e.g., ``2gmz``. :param mark_read: If True, conversation is marked as read (default: False). - :param fetch: If True, conversation fully fetched (default: True). + :param fetch: Determines if Async PRAW will fetch the object (default: False). For example: @@ -3360,7 +3360,7 @@ async def __call__( .. code-block:: python subreddit = await reddit.subreddit("redditdev") - message = await subreddit.modmail("2gmz", lazy=True) + message = await subreddit.modmail("2gmz", fetch=False) await message.archive() To print all messages from a conversation as Markdown source: @@ -4083,10 +4083,12 @@ async def upload_mobile_icon(self, image_path: str) -> Dict[str, str]: class SubredditWiki: """Provides a set of wiki functions to a Subreddit.""" - async def get_page(self, page_name, lazy=False) -> WikiPage: + @deprecate_lazy + async def get_page(self, page_name, fetch: bool = True, **kwargs) -> WikiPage: """Return the WikiPage for the subreddit named ``page_name``. - Set ``lazy=True`` to skip fetching the wiki page. + :param page_name: Name of the wikipage. + :param fetch: Determines if Async PRAW will fetch the object (default: True). This method is to be used to fetch a specific wikipage, like so: @@ -4098,7 +4100,7 @@ async def get_page(self, page_name, lazy=False) -> WikiPage: """ wikipage = WikiPage(self.subreddit._reddit, self.subreddit, page_name.lower()) - if not lazy: + if fetch: await wikipage._fetch() return wikipage diff --git a/asyncpraw/models/reddit/user_subreddit.py b/asyncpraw/models/reddit/user_subreddit.py index 64dded90..d01e8e2a 100644 --- a/asyncpraw/models/reddit/user_subreddit.py +++ b/asyncpraw/models/reddit/user_subreddit.py @@ -98,7 +98,7 @@ def wrapper(*args, **kwargs): warn( "`Redditor.subreddit` is no longer a dict and is now an `UserSubreddit`" f" object. Using `{func.__name__}` is deprecated and will be removed in" - f" Async PRAW 8.", + " Async PRAW 8.", category=DeprecationWarning, stacklevel=2, ) diff --git a/asyncpraw/models/reddit/wikipage.py b/asyncpraw/models/reddit/wikipage.py index bef1b882..45be91e6 100644 --- a/asyncpraw/models/reddit/wikipage.py +++ b/asyncpraw/models/reddit/wikipage.py @@ -51,7 +51,7 @@ async def add(self, redditor: "asyncpraw.models.Redditor"): .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("praw_test", lazy=True) + page = await subreddit.wiki.get_page("praw_test", fetch=False) await page.mod.add("spez") """ @@ -72,7 +72,7 @@ async def remove(self, redditor: "asyncpraw.models.Redditor"): .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("praw_test", lazy=True) + page = await subreddit.wiki.get_page("praw_test", fetch=False) await page.mod.remove("spez") """ @@ -162,7 +162,7 @@ async def update( .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("praw_test", lazy=True) + page = await subreddit.wiki.get_page("praw_test", fetch=False) await page.mod.update(listed=False, permlevel=2) """ @@ -233,7 +233,7 @@ def mod(self) -> WikiPageModeration: .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("praw_test", lazy=True) + page = await subreddit.wiki.get_page("praw_test", fetch=False) await page.mod.add("spez") """ @@ -306,7 +306,7 @@ async def edit( .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("test", lazy=True) + page = await subreddit.wiki.get_page("test", fetch=False) await page.edit(content="test wiki page") """ @@ -353,7 +353,7 @@ async def revision(self, revision: str): .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("praw_test", lazy=True) + page = await subreddit.wiki.get_page("praw_test", fetch=False) revision = await page.revision("[ID]") """ @@ -374,7 +374,7 @@ def revisions( .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("test_page", lazy=True) + page = await subreddit.wiki.get_page("test_page", fetch=False) async for item in page.revisions(): print(item) @@ -383,7 +383,7 @@ def revisions( .. code-block:: python subreddit = await reddit.subreddit("test") - page = await subreddit.wiki.get_page("test_page", lazy=True) + page = await subreddit.wiki.get_page("test_page", fetch=False) async for item in page.revisions(): print(item["page"]) diff --git a/asyncpraw/models/util.py b/asyncpraw/models/util.py index 9391f33b..5787c3fd 100644 --- a/asyncpraw/models/util.py +++ b/asyncpraw/models/util.py @@ -2,7 +2,9 @@ import asyncio import random from collections import OrderedDict +from functools import wraps from typing import Any, AsyncGenerator, Callable, List, Optional, Set +from warnings import warn class BoundedSet: @@ -62,6 +64,25 @@ def reset(self): self._base = 1 +def deprecate_lazy(func): # noqa: D401 + """A decorator used for deprecating the ``lazy`` keyword argument.""" + + @wraps(func) + def wrapper(*args, **kwargs): + if "lazy" in kwargs: + kwargs.setdefault("fetch", not kwargs.pop("lazy")) + warn( + "The parameter ``lazy`` has been renamed to ``fetch`` and support for" + " the ``lazy`` parameter will be removed in a future version of Async" + " PRAW.", + category=DeprecationWarning, + stacklevel=3, + ) + return func(*args, **kwargs) + + return wrapper + + def permissions_string( permissions: Optional[List[str]], known_permissions: Set[str] ) -> str: diff --git a/asyncpraw/reddit.py b/asyncpraw/reddit.py index 417149de..5025db94 100644 --- a/asyncpraw/reddit.py +++ b/asyncpraw/reddit.py @@ -40,6 +40,7 @@ MissingRequiredAttributeException, RedditAPIException, ) +from .models.util import deprecate_lazy from .objector import Objector from .util.token_manager import BaseTokenManager @@ -573,35 +574,37 @@ def _prepare_untrusted_asyncprawcore(self, requestor): self._read_only_core = session(read_only_authorizer) self._prepare_common_authorizer(authenticator) + @deprecate_lazy async def comment( self, # pylint: disable=invalid-name id: Optional[str] = None, # pylint: disable=redefined-builtin url: Optional[str] = None, - lazy: bool = False, + fetch: bool = True, + **kwargs, ): """Return an instance of :class:`~.Comment`. :param id: The ID of the comment. :param url: A permalink pointing to the comment. - :param lazy: If True, object is loaded lazily (default: False). + :param fetch: Determines if Async PRAW will fetch the object (default: True). If you don't need the object fetched right away (e.g., to utilize a class method) then you can do: .. code-block:: python - comment = await reddit.comment("comment_id", lazy=True) + comment = await reddit.comment("comment_id", fetch=False) await comment.reply("reply") .. note:: - If call this with ``lazy=True`` and you need to obtain the comment's - replies, you will need to call this without ``lazy=True`` or call + If call this with ``fetch=False`` and you need to obtain the comment's + replies, you will need to call this without ``fetch=False`` or call :meth:`~.Comment.refresh` on the returned :class:`.Comment`. """ comment = models.Comment(self, id=id, url=url) - if not lazy: + if fetch: await comment._fetch() return comment @@ -892,6 +895,7 @@ async def redditor( :param name: The name of the redditor. :param fullname: The fullname of the redditor, starting with ``t2_``. + :param fetch: Determines if Async PRAW will fetch the object (default: False). Either ``name`` or ``fullname`` can be provided, but not both. @@ -957,15 +961,19 @@ async def request( [data["reason"], explanation, field] ) from exception + @deprecate_lazy async def submission( # pylint: disable=invalid-name,redefined-builtin - self, id: Optional[str] = None, url: Optional[str] = None, lazy=False + self, + id: Optional[str] = None, + url: Optional[str] = None, + fetch: bool = True, + **kwargs, ) -> "asyncpraw.models.Submission": """Return an instance of :class:`~.Submission`. :param id: A Reddit base36 submission ID, e.g., ``2gmzqe``. - :param url: A URL supported by - :meth:`~asyncpraw.models.Submission.id_from_url`.`. - :param lazy: If True, object is loaded lazily (default: False). + :param url: A URL supported by :meth:`~asyncpraw.models.Submission.id_from_url`. + :param fetch: Determines if Async PRAW will fetch the object (default: True). Either ``id`` or ``url`` can be provided, but not both. @@ -974,12 +982,12 @@ async def submission( # pylint: disable=invalid-name,redefined-builtin .. code-block:: python - submission = await reddit.submission("submission_id", lazy=True) + submission = await reddit.submission("submission_id", fetch=False) await submission.mod.remove() """ submission = models.Submission(self, id=id, url=url) - if not lazy: + if fetch: await submission._fetch() return submission diff --git a/docs/examples/use_file_token_manager.py b/docs/examples/use_file_token_manager.py index de4c3d3a..1a527ba5 100755 --- a/docs/examples/use_file_token_manager.py +++ b/docs/examples/use_file_token_manager.py @@ -60,7 +60,8 @@ async def main(): print(f"{await reddit.user.me()} is authenticated with all scopes") elif "identity" in scopes: print( - f"{await reddit.user.me()} is authenticated with the following scopes: {scopes}" + f"{await reddit.user.me()} is authenticated with the following scopes:" + f" {scopes}" ) else: print(f"You are authenticated with the following scopes: {scopes}") diff --git a/docs/examples/use_sqlite_token_manager.py b/docs/examples/use_sqlite_token_manager.py index d4f31c3d..44dbdc21 100755 --- a/docs/examples/use_sqlite_token_manager.py +++ b/docs/examples/use_sqlite_token_manager.py @@ -43,7 +43,8 @@ async def main(): return 1 if len(sys.argv) != 2: sys.stderr.write( - "KEY must be provided.\n\nUsage: python3 use_sqlite_token_manager.py TOKEN_KEY\n" + "KEY must be provided.\n\nUsage: python3 use_sqlite_token_manager.py" + " TOKEN_KEY\n" ) return 1 @@ -62,7 +63,8 @@ async def main(): print(f"{await reddit.user.me()} is authenticated with all scopes") elif "identity" in scopes: print( - f"{await reddit.user.me()} is authenticated with the following scopes: {scopes}" + f"{await reddit.user.me()} is authenticated with the following scopes:" + f" {scopes}" ) else: print(f"You are authenticated with the following scopes: {scopes}") diff --git a/docs/getting_started/quick_start.rst b/docs/getting_started/quick_start.rst index 67afa097..bfb00179 100644 --- a/docs/getting_started/quick_start.rst +++ b/docs/getting_started/quick_start.rst @@ -300,5 +300,5 @@ For example: import pprint # assume you have a Reddit instance bound to variable `reddit` - submission = await reddit.submission(id="39zje0", lazy=False) + submission = await reddit.submission(id="39zje0") pprint.pprint(vars(submission)) diff --git a/docs/package_info/asyncpraw_migration.rst b/docs/package_info/asyncpraw_migration.rst index 42b370b8..bb4307b2 100644 --- a/docs/package_info/asyncpraw_migration.rst +++ b/docs/package_info/asyncpraw_migration.rst @@ -60,34 +60,34 @@ want to remove a post, you don't need the object fully fetched to do that. .. code-block:: python # network request is not made and object is lazily loaded - submission = await reddit.submission("id", lazy=True) + submission = await reddit.submission("id", fetch=False) # object is not fetched and is only removed await submission.mod.remove() The following objects are still lazily loaded by default: -- :class:`.Subreddit` -- :class:`.Redditor` - :class:`.LiveThread` - :class:`.Multireddit` +- :class:`.Redditor` +- :class:`.Subreddit` You can pass ``fetch=True`` in their respective helper method to fully load it. Inversely, the following objects are now fully fetched when initialized: -- :class:`.Submission` -- :class:`.Comment` -- :class:`.WikiPage` -- :class:`.RemovalReason` - :class:`.Collection` +- :class:`.Comment` - :class:`.Emoji` - :class:`.LiveUpdate` -- :class:`.Rule` - :class:`.Preferences` +- :class:`.RemovalReason` +- :class:`.Rule` +- :class:`.Submission` +- :class:`.WikiPage` -You can pass ``lazy=True`` in their respective helper method if you want to lazily load -it. +You can pass ``fetch=False`` in their respective helper method if you want to lazily +load it. In addition, there will be a ``load()`` method provided for manually fetching/refreshing objects that subclass :class:`.RedditBase`. If you need to later on access an attribute @@ -96,7 +96,7 @@ you need to call the ``.load()`` method first: .. code-block:: python # object is lazily loaded and no requests are made - submission = await reddit.submission("id", lazy=True) + submission = await reddit.submission("id", fetch=False) ... # network request is made and item is fully fetched await submission.load() @@ -109,8 +109,8 @@ Getting items by Indices .. _objects_by_indices: -In PRAW you could get specific :class:`.WikiPage`, :class:`.RemovalReason`, -:class:`.Emoji`, :class:`.LiveUpdate`, and :class:`.Rule` objects by using string +In PRAW you could get specific :class:`.Emoji`, :class:`.LiveUpdate`, +:class:`.RemovalReason`, :class:`.Rule`, and :class:`.WikiPage`, objects by using string indices. This will no longer work and has been converted to a ``.get_(item)`` method. Also, they are not lazily loaded by default anymore. diff --git a/tests/integration/models/reddit/test_emoji.py b/tests/integration/models/reddit/test_emoji.py index 9c0d6a98..a66d3253 100644 --- a/tests/integration/models/reddit/test_emoji.py +++ b/tests/integration/models/reddit/test_emoji.py @@ -23,13 +23,13 @@ async def test__fetch__invalid_emoji(self, _): subreddit = await self.reddit.subreddit(pytest.placeholders.test_subreddit) with pytest.raises(ClientException) as excinfo: await subreddit.emoji.get_emoji("invalid") - assert str(excinfo.value) == ( - f"r/{subreddit} does not have the emoji invalid" + assert ( + str(excinfo.value) == f"r/{subreddit} does not have the emoji invalid" ) with pytest.raises(ClientException) as excinfo2: await subreddit.emoji.get_emoji("Test_png") - assert str(excinfo2.value) == ( - f"r/{subreddit} does not have the emoji Test_png" + assert ( + str(excinfo2.value) == f"r/{subreddit} does not have the emoji Test_png" ) async def test_delete(self): diff --git a/tests/integration/models/reddit/test_removal_reasons.py b/tests/integration/models/reddit/test_removal_reasons.py index 4d1de9e2..af005ca1 100644 --- a/tests/integration/models/reddit/test_removal_reasons.py +++ b/tests/integration/models/reddit/test_removal_reasons.py @@ -41,8 +41,9 @@ async def test__fetch__invalid_reason(self, _): with self.use_cassette(): with pytest.raises(ClientException) as excinfo: await subreddit.mod.removal_reasons.get_reason("invalid") - assert str(excinfo.value) == ( - f"Subreddit {subreddit} does not have the removal reason invalid" + assert ( + str(excinfo.value) + == f"Subreddit {subreddit} does not have the removal reason invalid" ) @mock.patch("asyncio.sleep", return_value=None) diff --git a/tests/integration/models/reddit/test_subreddit.py b/tests/integration/models/reddit/test_subreddit.py index b6e2ca0f..4e1b2284 100644 --- a/tests/integration/models/reddit/test_subreddit.py +++ b/tests/integration/models/reddit/test_subreddit.py @@ -260,8 +260,12 @@ async def test_submit__selftext_inline_media(self, _): await submission.load() assert submission.author == pytest.placeholders.username assert ( - submission.selftext - == "Text with a gif\n\n[optional caption](https://i.redd.it/s1i7ejqkgdc61.gif)\n\nan image\n\n[optional caption](https://preview.redd.it/95pza2skgdc61.png?width=128&format=png&auto=webp&s=c81d303645d9792afcdb9c47f0a6039708714274)\n\nand a video\n\n[optional caption](https://reddit.com/link/l0vyxc/video/qeg2azskgdc61/player)\n\ninline" + submission.selftext == "Text with a gif\n\n[optional" + " caption](https://i.redd.it/s1i7ejqkgdc61.gif)\n\nan" + " image\n\n[optional" + " caption](https://preview.redd.it/95pza2skgdc61.png?width=128&format=png&auto=webp&s=c81d303645d9792afcdb9c47f0a6039708714274)\n\nand" + " a video\n\n[optional" + " caption](https://reddit.com/link/l0vyxc/video/qeg2azskgdc61/player)\n\ninline" ) assert submission.title == "title" diff --git a/tests/integration/test_reddit.py b/tests/integration/test_reddit.py index 82ed1efa..086f1b10 100644 --- a/tests/integration/test_reddit.py +++ b/tests/integration/test_reddit.py @@ -37,7 +37,8 @@ async def test_bad_request_without_json_text_html_response(self): ) assert ( str(excinfo.value) - == "

400 Bad request

\nYour browser sent an invalid request.\n\n" + == "

400 Bad request

\nYour browser sent an invalid" + " request.\n\n" ) async def test_bare_badrequest(self): diff --git a/tests/unit/models/reddit/test_emoji.py b/tests/unit/models/reddit/test_emoji.py index 1a8b243c..2dee100f 100644 --- a/tests/unit/models/reddit/test_emoji.py +++ b/tests/unit/models/reddit/test_emoji.py @@ -27,7 +27,7 @@ def test_equality(self): async def test__get(self): subreddit = Subreddit(self.reddit, display_name="a") - emoji = await subreddit.emoji.get_emoji("a", lazy=True) + emoji = await subreddit.emoji.get_emoji("a", fetch=False) assert isinstance(emoji, Emoji) def test_hash(self): diff --git a/tests/unit/models/reddit/test_live.py b/tests/unit/models/reddit/test_live.py index 66704669..24d4698b 100644 --- a/tests/unit/models/reddit/test_live.py +++ b/tests/unit/models/reddit/test_live.py @@ -64,7 +64,7 @@ async def test_getitem(self): thread_id = "dummy_thread_id" update_id = "dummy_update_id" thread = LiveThread(self.reddit, id=thread_id) - update = await thread.get_update(update_id, lazy=True) + update = await thread.get_update(update_id, fetch=False) assert isinstance(update, LiveUpdate) assert update.id == update_id diff --git a/tests/unit/models/reddit/test_removal_reasons.py b/tests/unit/models/reddit/test_removal_reasons.py index 5e73a208..b12367cb 100644 --- a/tests/unit/models/reddit/test_removal_reasons.py +++ b/tests/unit/models/reddit/test_removal_reasons.py @@ -59,7 +59,9 @@ def test_exception(self): async def test__get(self): subreddit = Subreddit(self.reddit, display_name="a") - removal_reason = await subreddit.mod.removal_reasons.get_reason("a", lazy=True) + removal_reason = await subreddit.mod.removal_reasons.get_reason( + "a", fetch=False + ) assert isinstance(removal_reason, RemovalReason) def test_hash(self): diff --git a/tests/unit/models/reddit/test_submission.py b/tests/unit/models/reddit/test_submission.py index f5214b32..8b1b153e 100644 --- a/tests/unit/models/reddit/test_submission.py +++ b/tests/unit/models/reddit/test_submission.py @@ -50,7 +50,7 @@ def test_construct_failure(self): @pytest.mark.filterwarnings("error", category=UserWarning) async def test_comment_sort_warning(self): with pytest.raises(UserWarning) as excinfo: - submission = await self.reddit.submission("1234", lazy=True) + submission = await self.reddit.submission("1234", fetch=False) submission._fetched = True submission.comment_sort = "new" assert ( @@ -63,7 +63,7 @@ async def test_comment_sort_warning(self): @pytest.LogCaptureFixture async def test_comment_sort_warning__disabled(self, caplog): self.reddit.config.warn_comment_sort = False - submission = await self.reddit.submission("1234", lazy=False) + submission = await self.reddit.submission("1234") submission._fetched = True submission.comment_sort = "new" assert caplog.records == [] diff --git a/tests/unit/models/reddit/test_subreddit.py b/tests/unit/models/reddit/test_subreddit.py index dd2669ef..6b9bce7e 100644 --- a/tests/unit/models/reddit/test_subreddit.py +++ b/tests/unit/models/reddit/test_subreddit.py @@ -141,7 +141,10 @@ async def test_submit_gallery__invalid_path(self): async def test_submit_gallery__too_long_caption(self): message = "Caption must be 180 characters or less." subreddit = Subreddit(self.reddit, display_name="name") - caption = "wayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy too long caption" + caption = ( + "wayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" + " too long caption" + ) with pytest.raises(TypeError) as excinfo: await subreddit.submit_gallery( "Cool title", images=[{"image_path": __file__, "caption": caption}] @@ -181,7 +184,7 @@ async def test_not_implemented(self): class TestSubredditWiki(UnitTest): async def test__getitem(self): subreddit = Subreddit(self.reddit, display_name="name") - wikipage = await subreddit.wiki.get_page("Foo", lazy=True) + wikipage = await subreddit.wiki.get_page("Foo", fetch=False) assert isinstance(wikipage, WikiPage) assert "foo" == wikipage.name diff --git a/tests/unit/models/reddit/test_wikipage.py b/tests/unit/models/reddit/test_wikipage.py index 9d5dac64..6985bb1b 100644 --- a/tests/unit/models/reddit/test_wikipage.py +++ b/tests/unit/models/reddit/test_wikipage.py @@ -34,9 +34,7 @@ def test_hash(self): def test_repr(self): page = WikiPage(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") - assert repr(page) == ( - "WikiPage(subreddit=Subreddit(display_name='a'), name='x')" - ) + assert repr(page) == "WikiPage(subreddit=Subreddit(display_name='a'), name='x')" def test_str(self): page = WikiPage(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") diff --git a/tests/unit/test_deprecations.py b/tests/unit/test_deprecations.py index 8fbe888d..6f05acb1 100644 --- a/tests/unit/test_deprecations.py +++ b/tests/unit/test_deprecations.py @@ -17,15 +17,6 @@ @pytest.mark.filterwarnings("error", category=DeprecationWarning) class TestDeprecation(UnitTest): - def test_validate_on_submit(self): - with pytest.raises(DeprecationWarning): - self.reddit.validate_on_submit - self.reddit.validate_on_submit = True - assert self.reddit.validate_on_submit - self.reddit.validate_on_submit = False - with pytest.raises(DeprecationWarning): - self.reddit.validate_on_submit - def test_api_exception(self): exc = APIException(["test", "testing", "test"]) with pytest.raises(DeprecationWarning): @@ -35,6 +26,29 @@ def test_api_exception(self): with pytest.raises(DeprecationWarning): exc.field + async def test_gild_method(self): + with pytest.raises(DeprecationWarning) as excinfo: + submission = await self.reddit.submission("1234", fetch=False) + await submission.gild() + assert excinfo.value.args[0] == "`.gild` has been renamed to `.award`." + + def test_gold_method(self): + with pytest.raises(DeprecationWarning) as excinfo: + self.reddit.subreddits.gold() + assert ( + excinfo.value.args[0] + == "`subreddits.gold` has be renamed to `subreddits.premium`." + ) + + async def test_lazy_argument_rename(self): + with pytest.deprecated_call() as warning_info: + await self.reddit.submission("1234", lazy=True) + assert ( + str(warning_info.list[0].message) + == "The parameter ``lazy`` has been renamed to ``fetch`` and support for" + " the ``lazy`` parameter will be removed in a future version of Async PRAW." + ) + def test_praw_exception_rename(self): with pytest.raises(AsyncPRAWException): Reddit() @@ -52,42 +66,6 @@ def test_praw_exception_rename(self): with pytest.raises(DeprecationWarning): from asyncpraw.exceptions import PRAWException # noqa: F401 - async def test_subreddit_rules_call(self): - with pytest.raises(DeprecationWarning) as excinfo: - subreddit = Subreddit(self.reddit, display_name="test") - await subreddit.rules() - assert ( - excinfo.value.args[0] - == "Calling SubredditRules to get a list of rules is deprecated. Remove the parentheses to use the iterator. View the Async PRAW documentation on how to change the code in order to use the iterator (https://asyncpraw.readthedocs.io/en/latest/code_overview/other/subredditrules.html#asyncpraw.models.reddit.rules.SubredditRules.__call__)." - ) - - def test_web_socket_exception_attribute(self): - exc = WebSocketException("Test", Exception("Test")) - with pytest.raises(DeprecationWarning) as excinfo: - _ = exc.original_exception - assert ( - excinfo.value.args[0] - == "Accessing the attribute original_exception is deprecated. Please rewrite your code in such a way that this attribute does not need to be used. It will be removed in Async PRAW 8.0." - ) - - def test_gold_method(self): - with pytest.raises(DeprecationWarning) as excinfo: - self.reddit.subreddits.gold() - assert ( - excinfo.value.args[0] - == "`subreddits.gold` has be renamed to `subreddits.premium`." - ) - - async def test_gild_method(self): - with pytest.raises(DeprecationWarning) as excinfo: - submission = await self.reddit.submission("1234", lazy=True) - await submission.gild() - assert excinfo.value.args[0] == "`.gild` has been renamed to `.award`." - - async def test_reddit_user_me_read_only(self): - with pytest.raises(DeprecationWarning): - await self.reddit.user.me() - async def test_reddit_token_manager(self): with pytest.raises(DeprecationWarning): async with Reddit( @@ -99,13 +77,31 @@ async def test_reddit_token_manager(self): ) as reddit: reddit._core._requestor._http = None + async def test_reddit_user_me_read_only(self): + with pytest.raises(DeprecationWarning): + await self.reddit.user.me() + + async def test_subreddit_rules_call(self): + with pytest.raises(DeprecationWarning) as excinfo: + subreddit = Subreddit(self.reddit, display_name="test") + await subreddit.rules() + assert ( + excinfo.value.args[0] + == "Calling SubredditRules to get a list of rules is deprecated. Remove the" + " parentheses to use the iterator. View the Async PRAW documentation on how" + " to change the code in order to use the iterator" + " (https://asyncpraw.readthedocs.io/en/latest/code_overview/other/subredditrules.html#asyncpraw.models.reddit.rules.SubredditRules.__call__)." + ) + async def test_synchronous_context_manager(self): with pytest.raises(DeprecationWarning) as excinfo: with self.reddit: pass assert ( excinfo.value.args[0] - == "Using this class as a synchronous context manager is deprecated and will be removed in the next release. Use this class as an asynchronous context manager instead." + == "Using this class as a synchronous context manager is deprecated and" + " will be removed in the next release. Use this class as an" + " asynchronous context manager instead." ) def test_user_subreddit_as_dict(self): @@ -115,10 +111,34 @@ def test_user_subreddit_as_dict(self): assert display_name == "test" assert ( warning_info.list[0].message.args[0] - == "`Redditor.subreddit` is no longer a dict and is now an `UserSubreddit` object. Accessing attributes using string indices is deprecated." + == "`Redditor.subreddit` is no longer a dict and is now an" + " `UserSubreddit` object. Accessing attributes using string indices is" + " deprecated." ) assert user_subreddit.keys() == user_subreddit.__dict__.keys() assert ( warning_info.list[1].message.args[0] - == "`Redditor.subreddit` is no longer a dict and is now an `UserSubreddit` object. Using `keys` is deprecated and will be removed in Async PRAW 8." + == "`Redditor.subreddit` is no longer a dict and is now an" + " `UserSubreddit` object. Using `keys` is deprecated and will be" + " removed in Async PRAW 8." ) + + def test_validate_on_submit(self): + with pytest.raises(DeprecationWarning): + self.reddit.validate_on_submit + self.reddit.validate_on_submit = True + assert self.reddit.validate_on_submit + self.reddit.validate_on_submit = False + with pytest.raises(DeprecationWarning): + self.reddit.validate_on_submit + + def test_web_socket_exception_attribute(self): + exc = WebSocketException("Test", Exception("Test")) + with pytest.raises(DeprecationWarning) as excinfo: + _ = exc.original_exception + assert ( + excinfo.value.args[0] + == "Accessing the attribute original_exception is deprecated. Please" + " rewrite your code in such a way that this attribute does not need to" + " be used. It will be removed in Async PRAW 8.0." + ) diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 8fae286b..9a090fa1 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -49,7 +49,8 @@ def test_repr(self): error = RedditErrorItem("BAD_SOMETHING", "invalid something", "some_field") assert ( repr(error) - == "RedditErrorItem(error_type='BAD_SOMETHING', message='invalid something', field='some_field')" + == "RedditErrorItem(error_type='BAD_SOMETHING', message='invalid" + " something', field='some_field')" ) @@ -100,7 +101,8 @@ def test_inheritance(self): def test_message(self): assert ( str(DuplicateReplaceException()) - == "A duplicate comment has been detected. Are you attempting to call ``replace_more_comments`` more than once?" + == "A duplicate comment has been detected. Are you attempting to call" + " ``replace_more_comments`` more than once?" ) @@ -111,7 +113,8 @@ def test_inheritance(self): def test_str(self): assert ( str(InvalidFlairTemplateID("123")) - == "The flair template id ``123`` is invalid. If you are trying to create a flair, please use the ``add`` method." + == "The flair template id ``123`` is invalid. If you are trying to create a" + " flair, please use the ``add`` method." ) @@ -184,5 +187,7 @@ def test_inheritance(self): def test_message(self): assert ( str(MediaPostFailed()) - == "The attempted media upload action has failed. Possible causes include the corruption of media files. Check that the media file can be opened on your local machine." + == "The attempted media upload action has failed. Possible causes include" + " the corruption of media files. Check that the media file can be opened" + " on your local machine." ) diff --git a/tests/unit/test_reddit.py b/tests/unit/test_reddit.py index 6d645554..23a4dd16 100644 --- a/tests/unit/test_reddit.py +++ b/tests/unit/test_reddit.py @@ -64,7 +64,8 @@ def test_conflicting_settings(self): ) assert ( str(excinfo.value) - == "``refresh_token`` setting cannot be provided when providing ``token_manager``" + == "``refresh_token`` setting cannot be provided when providing" + " ``token_manager``" ) async def test_context_manager(self): @@ -90,13 +91,15 @@ def test_invalid_config(self): Reddit(timeout="test", **self.REQUIRED_DUMMY_SETTINGS) assert ( excinfo.value.args[0] - == "An incorrect config type was given for option timeout. The expected type is int, but the given value is test." + == "An incorrect config type was given for option timeout. The expected" + " type is int, but the given value is test." ) with pytest.raises(ValueError) as excinfo: Reddit(ratelimit_seconds="test", **self.REQUIRED_DUMMY_SETTINGS) assert ( excinfo.value.args[0] - == "An incorrect config type was given for option ratelimit_seconds. The expected type is int, but the given value is test." + == "An incorrect config type was given for option ratelimit_seconds. The" + " expected type is int, but the given value is test." ) def test_info__not_list(self): @@ -464,7 +467,7 @@ async def test_request__json_and_body(self): ) async def test_submission(self): - submission = await self.reddit.submission("2gmzqe", lazy=True) + submission = await self.reddit.submission("2gmzqe", fetch=False) assert submission.id == "2gmzqe" async def test_subreddit(self):