From c63ed8559e04b098f965aa94ad5f3644ffca5fad Mon Sep 17 00:00:00 2001 From: Clayton A Davis Date: Wed, 6 Sep 2017 18:30:32 -0400 Subject: [PATCH 1/2] Improve error handling for api.cursor --- twython/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/twython/api.py b/twython/api.py index 954033f..5f0c50d 100644 --- a/twython/api.py +++ b/twython/api.py @@ -470,6 +470,11 @@ class Twython(EndpointsMixin, object): >>> print result """ + if not callable(function): + raise TypeError('.cursor() takes a Twython function as its first \ + argument. Did you provide the result of a \ + function call?') + if not hasattr(function, 'iter_mode'): raise TwythonError('Unable to create generator for Twython \ method "%s"' % function.__name__) From 6166e86807fdf474c6521ed3b818d2a28cbda687 Mon Sep 17 00:00:00 2001 From: Clayton A Davis Date: Mon, 11 Sep 2017 13:52:43 -0400 Subject: [PATCH 2/2] Add test for cursor creation --- tests/test_core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 2a30df5..44adf95 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -356,3 +356,15 @@ class TwythonAPITestCase(unittest.TestCase): tweet_text = self.api.html_for_tweet(test_tweet_extended_object) # full tweet rendered with suffix self.assertEqual(test_tweet_extended_html, tweet_text) + + def test_cursor_requires_twython_function(self): + """Test that cursor() raises when called without a Twython function""" + def init_and_iterate_cursor(*args, **kwargs): + cursor = self.api.cursor(*args, **kwargs) + return next(cursor) + + non_function = object() + non_twython_function = lambda x: x + + self.assertRaises(TypeError, init_and_iterate_cursor, non_function) + self.assertRaises(TwythonError, init_and_iterate_cursor, non_twython_function)