Skip to content

Commit

Permalink
Add ability to leverage PRAW8's ReadOnlyException in user.me
Browse files Browse the repository at this point in the history
(cherry picked from commit bb0e00f7ec628f7dfcd0396f3da323c121f22aa1)
  • Loading branch information
bboe authored and LilSpazJoekp committed Feb 23, 2021
1 parent 9fdb7f7 commit f101446
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Change Log
Unreleased
----------

**Deprecated**

* :meth:`.me` will no longer return ``None`` when called in read-only mode starting in
Async PRAW 8. A ``DeprecationWarning`` will be issued. To switch forward to the Async
PRAW 8 behavior set ``praw8_raise_exception_on_me=True`` in your
``asyncpraw.Reddit(...)`` call.

7.1.1 (2021/02/11)
------------------

Expand Down
4 changes: 4 additions & 0 deletions asyncpraw/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ class MissingRequiredAttributeException(ClientException):
"""Indicate exceptions caused by not including a required attribute."""


class ReadOnlyException(ClientException):
"""Raised when a method call requires read_only mode to be disabled."""


class TooLargeMediaException(ClientException):
"""Indicate exceptions from uploading media that's too large."""

Expand Down
23 changes: 14 additions & 9 deletions asyncpraw/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from warnings import warn

from ..const import API_PATH
from ..exceptions import ReadOnlyException
from ..models import Preferences
from ..util.cache import cachedproperty
from .base import AsyncPRAWBase
Expand Down Expand Up @@ -129,18 +130,22 @@ async def me(
.. deprecated:: 7.2
In read-only mode this method returns ``None``. In Async PRAW 8 this method
will raise :class:`.ReadOnlyException` when called in read-only mode.
will raise :class:`.ReadOnlyException` when called in read-only mode. To
operate in Async PRAW 8 mode, set the config variable
``praw8_raise_exception_on_me`` to True.
"""
if self._reddit.read_only:
warn(
"The `None` return value is deprecated, and will raise a "
"`ReadOnlyException` beginning with Async PRAW 8. See documentation "
"for forward compatibility options.",
category=DeprecationWarning,
stacklevel=2,
)
return None
if not self._reddit.config.custom.get("praw8_raise_exception_on_me"):
warn(
"The `None` return value is deprecated, and will raise a "
"`ReadOnlyException` beginning with Async PRAW 8. See "
"documentation for forward compatibility options.",
category=DeprecationWarning,
stacklevel=2,
)
return None
raise ReadOnlyException("`user.me()` does not work in read_only mode")
if "_me" not in self.__dict__ or not use_cache:
user_data = await self._reddit.get(API_PATH["me"])
self._me = Redditor(self._reddit, _data=user_data)
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/models/test_user.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import pytest

from asyncpraw import Reddit
from asyncpraw.exceptions import ReadOnlyException
from asyncpraw.models import User

from .. import UnitTest


class TestUser(UnitTest):
async def test_me__in_read_only_mode(self):
self.reddit = Reddit(
client_id="dummy",
client_secret="dummy",
praw8_raise_exception_on_me=True,
user_agent="dummy",
)
self.reddit._core._requestor._http = None
assert self.reddit.read_only
user = User(self.reddit)
assert await user.me() is None
with pytest.raises(ReadOnlyException):
await user.me()

async def test_me__in_read_only_mode__deprecated(self):
assert self.reddit.read_only
assert await User(self.reddit).me() is None

0 comments on commit f101446

Please sign in to comment.