Repository: praw-dev/praw

Issue:
Enable Subreddit instances to be passed to `subreddit.filters.add`
In most places where a string representing the name of a subreddit, or the name of a redditor is required by Reddit's API we pass the value in as `str(instance)` so that the caller has the option of passing in either the string, or an instance of the appropriate object.

It would seem, however, that `subreddit.filters.add` does not wrap the `subreddit` in such a call, and for consistency it would be good if it did.

See the following discussion for more information: https://www.reddit.com/r/redditdev/comments/8brc3y/how_to_filter_subreddits_when_streaming/dx8zdkg/
Enable Subreddit instances to be passed to `subreddit.filters.add`
In most places where a string representing the name of a subreddit, or the name of a redditor is required by Reddit's API we pass the value in as `str(instance)` so that the caller has the option of passing in either the string, or an instance of the appropriate object.

It would seem, however, that `subreddit.filters.add` does not wrap the `subreddit` in such a call, and for consistency it would be good if it did.

See the following discussion for more information: https://www.reddit.com/r/redditdev/comments/8brc3y/how_to_filter_subreddits_when_streaming/dx8zdkg/


Discussion:

Resolving patch:
diff --git a/praw/models/reddit/subreddit.py b/praw/models/reddit/subreddit.py
--- a/praw/models/reddit/subreddit.py
+++ b/praw/models/reddit/subreddit.py
@@ -593,7 +593,7 @@ def add(self, subreddit):
             special=self.subreddit, user=self.subreddit._reddit.user.me(),
             subreddit=subreddit)
         self.subreddit._reddit.request(
-            'PUT', url, data={'model': dumps({'name': subreddit})})
+            'PUT', url, data={'model': dumps({'name': str(subreddit)})})
 
     def remove(self, subreddit):
         """Remove ``subreddit`` from the list of filtered subreddits.
@@ -605,7 +605,7 @@ def remove(self, subreddit):
         """
         url = API_PATH['subreddit_filter'].format(
             special=self.subreddit, user=self.subreddit._reddit.user.me(),
-            subreddit=subreddit)
+            subreddit=str(subreddit))
         self.subreddit._reddit.request('DELETE', url, data={})
 
 


Test patch:
diff --git a/tests/integration/models/reddit/test_subreddit.py b/tests/integration/models/reddit/test_subreddit.py
--- a/tests/integration/models/reddit/test_subreddit.py
+++ b/tests/integration/models/reddit/test_subreddit.py
@@ -218,6 +218,13 @@ def test_add(self, _):
         with self.recorder.use_cassette('TestSubredditFilters.test_add'):
             self.reddit.subreddit('all').filters.add('redditdev')
 
+    @mock.patch('time.sleep', return_value=None)
+    def test_add__subreddit_model(self, _):
+        self.reddit.read_only = False
+        with self.recorder.use_cassette('TestSubredditFilters.test_add'):
+            self.reddit.subreddit('all').filters.add(
+                self.reddit.subreddit('redditdev'))
+
     @mock.patch('time.sleep', return_value=None)
     def test_add__non_special(self, _):
         self.reddit.read_only = False
@@ -232,6 +239,13 @@ def test_remove(self, _):
         with self.recorder.use_cassette('TestSubredditFilters.test_remove'):
             self.reddit.subreddit('mod').filters.remove('redditdev')
 
+    @mock.patch('time.sleep', return_value=None)
+    def test_remove__subreddit_model(self, _):
+        self.reddit.read_only = False
+        with self.recorder.use_cassette('TestSubredditFilters.test_remove'):
+            self.reddit.subreddit('mod').filters.remove(
+                self.reddit.subreddit('redditdev'))
+
     @mock.patch('time.sleep', return_value=None)
     def test_remove__non_special(self, _):
         self.reddit.read_only = False