Skip to content

Commit

Permalink
Merge pull request #116 from praw-dev/fix_user_flair
Browse files Browse the repository at this point in the history
Fix RedditBase object being sent as "None"
  • Loading branch information
LilSpazJoekp authored Jul 6, 2021
2 parents 2657a1a + cf2316b commit d0aa191
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 112 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ Async PRAW follows `semantic versioning <http://semver.org/>`_.
Unreleased
----------

**Changed**

- :class:`.Reddit` will now be shallow copied when a deepcopy is preformed on it as
``asyncprawcore.Session`` (more specifically, :py:class:`asyncio.AbstractEventLoop`)
does not support being deepcopied.

**Fixed**

- Fixed an issue where some :class:`.RedditBase` objects would be sent in a request as
``"None"``.

7.3.0 (2021/06/18)
------------------

Expand Down
8 changes: 0 additions & 8 deletions asyncpraw/models/reddit/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Provide the RedditBase class."""
from copy import deepcopy
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from urllib.parse import urlparse

Expand All @@ -13,13 +12,6 @@
class RedditBase(AsyncPRAWBase):
"""Base class that represents actual Reddit objects."""

def __deepcopy__(self, memodict={}):
"""Skip copying ``_reddit`` attribute when preforming a deep copy."""
return type(self)(
self._reddit,
_data=deepcopy({k: v for k, v in self.__dict__.items() if k != "_reddit"}),
)

@staticmethod
def _url_parts(url):
parsed = urlparse(url)
Expand Down
4 changes: 2 additions & 2 deletions asyncpraw/models/reddit/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async def set_original_content(self):
"fullname": self.thing.fullname,
"should_set_oc": True,
"executed": False,
"r": str(self.thing.subreddit),
"r": self.thing.subreddit,
}
await self.thing._reddit.post(API_PATH["set_original_content"], data=data)

Expand Down Expand Up @@ -333,7 +333,7 @@ async def unset_original_content(self):
"fullname": self.thing.fullname,
"should_set_oc": False,
"executed": False,
"r": str(self.thing.subreddit),
"r": self.thing.subreddit,
}
await self.thing._reddit.post(API_PATH["set_original_content"], data=data)

Expand Down
10 changes: 5 additions & 5 deletions asyncpraw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ def __call__(
print(flair)
"""
Subreddit._safely_add_arguments(generator_kwargs, "params", name=str(redditor))
Subreddit._safely_add_arguments(generator_kwargs, "params", name=redditor)
generator_kwargs.setdefault("limit", None)
url = API_PATH["flairlist"].format(subreddit=self.subreddit)
return ListingGenerator(self.subreddit._reddit, url, **generator_kwargs)
Expand Down Expand Up @@ -2903,7 +2903,7 @@ async def opt_in(self):
print(submission) # Returns Submission
"""
data = {"sr_name": str(self.subreddit)}
data = {"sr_name": self.subreddit}
try:
await self.subreddit._reddit.post(API_PATH["quarantine_opt_in"], data=data)
except Redirect:
Expand All @@ -2925,7 +2925,7 @@ async def opt_out(self):
print(submission)
"""
data = {"sr_name": str(self.subreddit)}
data = {"sr_name": self.subreddit}
try:
await self.subreddit._reddit.post(API_PATH["quarantine_opt_out"], data=data)
except Redirect:
Expand Down Expand Up @@ -3499,9 +3499,9 @@ async def create(
data = {
"body": body,
"isAuthorHidden": author_hidden,
"srName": str(self.subreddit),
"srName": self.subreddit,
"subject": subject,
"to": str(recipient),
"to": recipient,
}
return await self.subreddit._reddit.post(
API_PATH["modmail_conversations"], data=data
Expand Down
10 changes: 10 additions & 0 deletions asyncpraw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import configparser
import os
import re
from copy import copy
from itertools import islice
from logging import getLogger
from typing import (
Expand Down Expand Up @@ -144,6 +145,15 @@ async def __aexit__(self, *_args):
"""Handle the context manager close."""
await self.close()

def __deepcopy__(self, memodict={}):
"""Shallow copy on deepcopy.
A shallow copied is performed on deepcopy as
:py:class:`asyncio.AbstractEventLoop` cannot be deepcopied.
"""
return copy(self)

def __enter__(self):
"""Handle the context manager open.
Expand Down
53 changes: 27 additions & 26 deletions tests/integration/cassettes/TestSubredditFlair.test__call.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
"body": [
[
"grant_type",
"refresh_token"
"password"
],
[
"refresh_token",
"<REFRESH_TOKEN>"
"password",
"<PASSWORD>"
],
[
"username",
"<USERNAME>"
]
],
"headers": {
Expand All @@ -23,34 +27,34 @@
"close"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.1.1.dev0 asyncprawcore/1.4.0.post1"
"<USER_AGENT> Async PRAW/7.3.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"account creddits edit flair history identity livemanage modconfig modcontributors modflair modlog modmail modothers modposts modself modtraffic modwiki mysubreddits privatemessages read report save structuredstyles submit subscribe vote wikiedit wikiread\"}"
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "max-age=0, must-revalidate",
"Connection": "close",
"Content-Length": "367",
"Content-Length": "121",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Sat, 11 Jul 2020 04:06:42 GMT",
"Date": "Tue, 06 Jul 2021 20:00:27 GMT",
"Server": "snooserv",
"Set-Cookie": "edgebucket=fgopez0X5Oe6uN6Z9r; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Set-Cookie": "edgebucket=H5RxIpRbXgkx2oOMGi; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"X-Served-By": "cache-mci5927-MCI",
"X-Timer": "S1594440402.372684,VS0,VE91",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "299",
"x-ratelimit-reset": "573",
"x-ratelimit-used": "1",
"x-xss-protection": "1; mode=block"
},
"status": {
Expand All @@ -71,49 +75,46 @@
"bearer <ACCESS_TOKEN>"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.1.1.dev0 asyncprawcore/1.4.0.post1"
"<USER_AGENT> Async PRAW/7.3.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "GET",
"uri": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/api/flairlist/?name=None&limit=1024&raw_json=1"
"uri": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/api/flairlist/?limit=1024&raw_json=1"
},
"response": {
"body": {
"string": "{\"users\": [{\"flair_css_class\": null, \"user\": \"None\", \"flair_text\": null}]}"
"string": "{\"users\": [{\"flair_css_class\": \"async default\", \"user\": \"spez\", \"flair_text\": null}, {\"flair_css_class\": \"async default\", \"user\": \"bsimpson\", \"flair_text\": null}, {\"flair_css_class\": \"async default\", \"user\": \"spladug\", \"flair_text\": \"a,b\"}, {\"flair_css_class\": \"testing\", \"user\": \"<USERNAME>\", \"flair_text\": \"\\\"testing\\\"\"}, {\"flair_css_class\": \"myCSS\", \"user\": \"Lil_SpazTest\", \"flair_text\": \"redditor flair\"}]}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate",
"Connection": "keep-alive",
"Content-Length": "74",
"Content-Length": "411",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Sat, 11 Jul 2020 04:06:42 GMT",
"Date": "Tue, 06 Jul 2021 20:00:28 GMT",
"Expires": "-1",
"Server": "snooserv",
"Set-Cookie": "csv=1; Max-Age=63072000; Domain=.reddit.com; Path=/; Secure; SameSite=None",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"X-Served-By": "cache-mci5931-MCI",
"X-Timer": "S1594440403.550532,VS0,VE117",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "224.0",
"x-ratelimit-reset": "198",
"x-ratelimit-used": "376",
"x-ratelimit-remaining": "599.0",
"x-ratelimit-reset": "572",
"x-ratelimit-used": "1",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/api/flairlist/?name=None&limit=1024&raw_json=1"
"url": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/api/flairlist/?limit=1024&raw_json=1"
}
}
],
"recorded_at": "2020-07-10T23:06:42",
"recorded_at": "2021-07-06T15:00:28",
"version": 1
}
Loading

0 comments on commit d0aa191

Please sign in to comment.