Skip to content

Commit

Permalink
Print warning when comment_sort is set after fetch
Browse files Browse the repository at this point in the history
(cherry picked from commit 4999b4e13c9423a9625a29dbca183a826b2e6935)
  • Loading branch information
Watchful1 authored and LilSpazJoekp committed Aug 4, 2021
1 parent c09c443 commit 3ef72e9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,15 @@ Unreleased

**Added**

- Log a warning if a submission's ``comment_sort`` attribute is updated after the
submission has already been fetched and a ``warn_comment_sort`` config setting to turn
off the warning.

7.4.0 (2021/07/30)
------------------

**Added**

- :meth:`~.WikiPage.discussions` to obtain site-wide link submissions that link to the
WikiPage.
- :meth:`.revert` to revert a WikiPage to a specified revision.
Expand Down
3 changes: 3 additions & 0 deletions asyncpraw/config.py
Expand Up @@ -114,6 +114,9 @@ def _initialize_attributes(self):
self.check_for_updates = self._config_boolean(
self._fetch_or_not_set("check_for_updates")
)
self.warn_comment_sort = self._config_boolean(
self._fetch_default("warn_comment_sort", True)
)
self.kinds = {
x: self._fetch(f"{x}_kind")
for x in [
Expand Down
12 changes: 12 additions & 0 deletions asyncpraw/models/reddit/submission.py
@@ -1,6 +1,7 @@
"""Provide the Submission class."""
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from urllib.parse import urljoin
from warnings import warn

from asyncprawcore import Conflict

Expand Down Expand Up @@ -619,6 +620,17 @@ def __setattr__(self, attribute: str, value: Any):
value = Subreddit(self._reddit, value)
elif attribute == "poll_data":
value = PollData(self._reddit, value)
elif (
attribute == "comment_sort"
and hasattr(self, "_fetched")
and self._fetched
and hasattr(self, "_reddit")
and self._reddit.config.warn_comment_sort
):
warn(
"The comments for this submission have already been fetched, "
"so the updated comment_sort will not have any effect"
)
super().__setattr__(attribute, value)

def _chunk(self, other_submissions, chunk_size):
Expand Down
2 changes: 2 additions & 0 deletions docs/getting_started/configuration/options.rst
Expand Up @@ -92,6 +92,8 @@ PRAW.
:timeout: Controls the amount of time Async PRAW will wait for a request from Reddit to
complete before throwing an exception. By default, Async PRAW waits 16 seconds
before throwing an exception.
:warn_comment_sort: When ``true``, log a warning when the ``comment_sort`` attribute of
a submission is updated after _fetch() has been called. (default: ``true``)

.. _custom_options:

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/models/reddit/test_submission.py
Expand Up @@ -47,6 +47,27 @@ def test_construct_failure(self):
with pytest.raises(ValueError):
Submission(self.reddit, url="")

@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._fetched = True
submission.comment_sort = "new"
assert (
excinfo.value.args[0]
== "The comments for this submission have already been fetched, "
"so the updated comment_sort will not have any effect"
)

@pytest.mark.filterwarnings("error", category=UserWarning)
@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._fetched = True
submission.comment_sort = "new"
assert caplog.records == []

def test_construct_from_url(self):
assert Submission(self.reddit, url="http://my.it/2gmzqe") == "2gmzqe"

Expand Down

0 comments on commit 3ef72e9

Please sign in to comment.