Skip to content

Commit

Permalink
Rename PRAWException to AsyncPRAWException
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Feb 8, 2021
1 parent b0a5477 commit c5ea5b5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -33,6 +33,7 @@ Unreleased
* :meth:`~.Subreddits.gold` is superseded by :meth:`~.Subreddits.premium`.
* :meth:`~.Submission.gild` is superseded by :meth:`~.Submission.award`.
* :meth:`~.Comment.gild` is superseded by :meth:`~.Comment.award`.
* ``PRAWException`` is superseded by :class:`.AsyncPRAWException`.

**Fixed**

Expand Down
38 changes: 33 additions & 5 deletions asyncpraw/exceptions.py
Expand Up @@ -3,19 +3,23 @@
Includes two main exceptions: :class:`.RedditAPIException` for when something
goes wrong on the server side, and :class:`.ClientException` when something
goes wrong on the client side. Both of these classes extend
:class:`.PRAWException`.
:class:`.AsyncPRAWException`.
All other exceptions are subclassed from :class:`.ClientException`.
"""
import sys
from typing import List, Optional, Union
from warnings import warn


class PRAWException(Exception):
class AsyncPRAWException(Exception):
"""The base Async PRAW Exception that all other exception classes extend."""


PRAWException = AsyncPRAWException


class RedditErrorItem:
"""Represents a single error returned from Reddit's API."""

Expand Down Expand Up @@ -57,7 +61,7 @@ def __str__(self):
return self.error_message


class APIException(PRAWException):
class APIException(AsyncPRAWException):
"""Old class preserved for alias purposes.
.. deprecated:: 7.0
Expand Down Expand Up @@ -103,7 +107,7 @@ def message(self) -> str:
Accessing attributes through instances of
:class:`.RedditAPIException` is deprecated. This behavior will be
removed in Async PRAW 8.0. Check out the
:ref:`PRAW 7 Migration tutorial <Exception_Handling>` on how to
:ref:`Async PRAW 7 Migration tutorial <Exception_Handling>` on how to
migrate code from this behavior.
"""
Expand Down Expand Up @@ -159,7 +163,7 @@ class RedditAPIException(APIException):
"""Container for error messages from Reddit's API."""


class ClientException(PRAWException):
class ClientException(AsyncPRAWException):
"""Indicate exceptions that don't involve interaction with Reddit's API."""


Expand Down Expand Up @@ -274,3 +278,27 @@ def __init__(self):
"file can be opened on your local machine.",
None,
)


# Adapted from https://stackoverflow.com/a/40546615
class ExceptionWrapper(object):
"""Wrapper to facilitate showing depreciation for PRAWException class rename."""

def __init__(self, wrapped):
"""Initialize Wrapper instance."""
self.wrapped = wrapped

def __getattr__(self, attribute):
"""Return the value of `attribute`."""
if attribute == "PRAWException":
warn(
"PRAWException as been renamed to AsyncPRAWException. PRAWException "
" will be removed in the next version Async PRAW.",
category=DeprecationWarning,
stacklevel=3,
)
return getattr(self.wrapped, attribute)


if "sphinx" not in sys.modules:
sys.modules[__name__] = ExceptionWrapper(sys.modules[__name__])
4 changes: 2 additions & 2 deletions tests/integration/models/reddit/test_comment.py
@@ -1,7 +1,7 @@
import pytest
from asynctest import mock

from asyncpraw.exceptions import ClientException, PRAWException, RedditAPIException
from asyncpraw.exceptions import AsyncPRAWException, ClientException, RedditAPIException
from asyncpraw.models import Comment, Submission

from ... import IntegrationTest
Expand Down Expand Up @@ -104,7 +104,7 @@ async def test_gild(self):

async def test_invalid(self):
with self.use_cassette():
with pytest.raises(PRAWException) as excinfo:
with pytest.raises(AsyncPRAWException) as excinfo:
await self.reddit.comment("0")
assert excinfo.value.args[0].startswith("No data returned for comment")

Expand Down
20 changes: 19 additions & 1 deletion tests/unit/test_deprecations.py
Expand Up @@ -2,7 +2,8 @@

import pytest

from asyncpraw.exceptions import APIException, WebSocketException
from asyncpraw import Reddit
from asyncpraw.exceptions import APIException, AsyncPRAWException, WebSocketException
from asyncpraw.models import Subreddit

from . import UnitTest
Expand All @@ -28,6 +29,23 @@ def test_api_exception(self):
with pytest.raises(DeprecationWarning):
exc.field

def test_praw_exception_rename(self):
with pytest.raises(AsyncPRAWException):
Reddit()

with pytest.raises(DeprecationWarning):
import asyncpraw

asyncpraw.exceptions.PRAWException

with pytest.raises(DeprecationWarning):
from asyncpraw import exceptions

exceptions.PRAWException

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")
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_exceptions.py
Expand Up @@ -3,27 +3,27 @@

from asyncpraw.exceptions import (
APIException,
AsyncPRAWException,
ClientException,
DuplicateReplaceException,
InvalidFlairTemplateID,
InvalidImplicitAuth,
InvalidURL,
MediaPostFailed,
MissingRequiredAttributeException,
PRAWException,
RedditAPIException,
RedditErrorItem,
WebSocketException,
)


class TestPRAWException:
class TestAsyncPRAWException:
def test_inheritance(self):
assert issubclass(PRAWException, Exception)
assert issubclass(AsyncPRAWException, Exception)

def test_str(self):
assert str(PRAWException()) == ""
assert str(PRAWException("foo")) == "foo"
assert str(AsyncPRAWException()) == ""
assert str(AsyncPRAWException("foo")) == "foo"


class TestRedditErrorItem:
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_catch(self):

class TestRedditAPIException:
def test_inheritance(self):
assert issubclass(RedditAPIException, PRAWException)
assert issubclass(RedditAPIException, AsyncPRAWException)

def test_items(self):
container = RedditAPIException(
Expand All @@ -86,7 +86,7 @@ def test_apiexception_value(self):

class TestClientException:
def test_inheritance(self):
assert issubclass(ClientException, PRAWException)
assert issubclass(ClientException, AsyncPRAWException)

def test_str(self):
assert str(ClientException()) == ""
Expand Down

0 comments on commit c5ea5b5

Please sign in to comment.