From 096c5ad3c20d849220fe9fe84d082f6fb612a09e Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 8 Sep 2017 10:28:10 +0100 Subject: [PATCH 1/2] Fixed #866 - Flak8 errors --- rq/compat/__init__.py | 8 ++++---- rq/connections.py | 2 +- rq/decorators.py | 2 +- rq/queue.py | 5 +++-- rq/worker.py | 6 +++--- tests/__init__.py | 2 +- tests/fixtures.py | 4 ++-- tests/test_job.py | 2 +- tests/test_queue.py | 6 +++--- tests/test_sentry.py | 2 +- tests/test_worker.py | 32 ++++++++++++++++++++------------ 11 files changed, 40 insertions(+), 31 deletions(-) diff --git a/rq/compat/__init__.py b/rq/compat/__init__.py index 7565835..9514aff 100644 --- a/rq/compat/__init__.py +++ b/rq/compat/__init__.py @@ -67,18 +67,18 @@ else: # Python 2.x def text_type(v): try: - return unicode(v) + return unicode(v) # noqa except Exception: - return unicode(v, "utf-8", errors="ignore") + return unicode(v, "utf-8", errors="ignore") # noqa - string_types = (str, unicode) + string_types = (str, unicode) # noqa def as_text(v): if v is None: return None elif isinstance(v, str): return v.decode('utf-8') - elif isinstance(v, unicode): + elif isinstance(v, unicode): # noqa return v else: raise Exception("Input cannot be decoded into literal thing.") diff --git a/rq/connections.py b/rq/connections.py index 03b8c05..d7f6a61 100644 --- a/rq/connections.py +++ b/rq/connections.py @@ -15,7 +15,7 @@ class NoRedisConnectionException(Exception): @contextmanager -def Connection(connection=None): +def Connection(connection=None): # noqa if connection is None: connection = StrictRedis() push_connection(connection) diff --git a/rq/decorators.py b/rq/decorators.py index f96ef93..2b43961 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -11,7 +11,7 @@ from .queue import Queue from .utils import backend_class -class job(object): +class job(object): # noqa queue_class = Queue def __init__(self, queue, connection=None, timeout=None, diff --git a/rq/queue.py b/rq/queue.py index 9d2357f..e694e3f 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -121,7 +121,8 @@ class Queue(object): except NoSuchJobError: self.remove(job_id) else: - if job.origin == self.name or (job.is_failed and self == get_failed_queue(connection=self.connection, job_class=self.job_class)): + if job.origin == self.name or \ + (job.is_failed and self == get_failed_queue(connection=self.connection, job_class=self.job_class)): return job def get_job_ids(self, offset=0, length=-1): @@ -168,7 +169,7 @@ class Queue(object): """Removes all "dead" jobs from the queue by cycling through it, while guaranteeing FIFO semantics. """ - COMPACT_QUEUE = 'rq:queue:_compact:{0}'.format(uuid.uuid4()) + COMPACT_QUEUE = 'rq:queue:_compact:{0}'.format(uuid.uuid4()) # noqa self.connection.rename(self.key, COMPACT_QUEUE) while True: diff --git a/rq/worker.py b/rq/worker.py index d163082..2a280cc 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -469,13 +469,13 @@ class Worker(object): if burst: self.log.info("RQ worker {0!r} done, quitting".format(self.key)) break - + job, queue = result self.execute_job(job, queue) self.heartbeat() - + did_perform_work = True - + except StopRequested: break finally: diff --git a/tests/__init__.py b/tests/__init__.py index 7371a92..c9acfa7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -70,7 +70,7 @@ class RQTestCase(unittest.TestCase): # Implement assertIsNotNone for Python runtimes < 2.7 or < 3.1 if not hasattr(unittest.TestCase, 'assertIsNotNone'): - def assertIsNotNone(self, value, *args): + def assertIsNotNone(self, value, *args): # noqa self.assertNotEqual(value, None, *args) @classmethod diff --git a/tests/fixtures.py b/tests/fixtures.py index f9d1eb1..4ef904a 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -29,7 +29,7 @@ def say_hello(name=None): def say_hello_unicode(name=None): """A job with a single argument and a return value.""" - return unicode(say_hello(name)) + return unicode(say_hello(name)) # noqa def do_nothing(): @@ -80,7 +80,7 @@ def modify_self_and_error(meta): def echo(*args, **kwargs): - return (args, kwargs) + return args, kwargs class Number(object): diff --git a/tests/test_job.py b/tests/test_job.py index 67b87e2..e715fbf 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -6,6 +6,7 @@ from datetime import datetime import time import sys + is_py2 = sys.version[0] == '2' if is_py2: import Queue as queue @@ -19,7 +20,6 @@ from rq.compat import PY2 from rq.exceptions import NoSuchJobError, UnpickleError from rq.job import Job, get_current_job, JobStatus, cancel_job, requeue_job from rq.queue import Queue, get_failed_queue -from rq.registry import DeferredJobRegistry from rq.utils import utcformat from rq.worker import Worker diff --git a/tests/test_queue.py b/tests/test_queue.py index 7129452..e19415f 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -655,11 +655,11 @@ class TestFailedQueue(RQTestCase): """Make sure failed job key does not expire""" q = Queue('foo') job = q.enqueue(div_by_zero, args=(1,), ttl=5) - self.assertEqual(self.testconn.ttl(job.key), 5) + self.assertEqual(self.testconn.ttl(job.key), 5) self.assertRaises(ZeroDivisionError, job.perform) job.set_status(JobStatus.FAILED) failed_queue = get_failed_queue() failed_queue.quarantine(job, Exception('Some fake error')) - - self.assertEqual(self.testconn.ttl(job.key), -1) + + self.assertEqual(self.testconn.ttl(job.key), -1) diff --git a/tests/test_sentry.py b/tests/test_sentry.py index 5933aee..7fa7bcb 100644 --- a/tests/test_sentry.py +++ b/tests/test_sentry.py @@ -11,7 +11,7 @@ from tests import RQTestCase class FakeSentry(object): servers = [] - def captureException(self, *args, **kwds): + def captureException(self, *args, **kwds): # noqa pass # we cannot check this, because worker forks diff --git a/tests/test_worker.py b/tests/test_worker.py index e29e371..9c5f3e6 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -259,10 +259,10 @@ class TestWorker(RQTestCase): job = Job.fetch(job.id) self.assertEqual(job.is_failed, True) - def test_cancelled_jobs_arent_executed(self): # noqa + def test_cancelled_jobs_arent_executed(self): """Cancelling jobs.""" - SENTINEL_FILE = '/tmp/rq-tests.txt' + SENTINEL_FILE = '/tmp/rq-tests.txt' # noqa try: # Remove the sentinel if it is leftover from a previous test run @@ -471,19 +471,27 @@ class TestWorker(RQTestCase): logging work properly""" q = Queue("foo") w = Worker([q]) - job = q.enqueue('tests.fixtures.say_hello', name='阿达姆', - description='你好 世界!') - self.assertEqual(w.work(burst=True), True, - 'Expected at least some work done.') - job = q.enqueue('tests.fixtures.say_hello_unicode', name='阿达姆', - description='你好 世界!') - self.assertEqual(w.work(burst=True), True, - 'Expected at least some work done.') + q.enqueue( + 'tests.fixtures.say_hello', + name='阿达姆', + description='你好 世界!') + self.assertEqual( + w.work(burst=True), + True, + 'Expected at least some work done.') + q.enqueue( + 'tests.fixtures.say_hello_unicode', + name='阿达姆', + description='你好 世界!') + self.assertEqual( + w.work(burst=True), + True, + 'Expected at least some work done.') def test_suspend_worker_execution(self): """Test Pause Worker Execution""" - SENTINEL_FILE = '/tmp/rq-tests.txt' + SENTINEL_FILE = '/tmp/rq-tests.txt' # noqa try: # Remove the sentinel if it is leftover from a previous test run @@ -919,7 +927,7 @@ class HerokuWorkerShutdownTestCase(TimeoutTestCase, RQTestCase): w = HerokuWorker('foo') w._horse_pid = 19999 - w.handle_warm_shutdown_request() + w.handle_warm_shutdown_request() class TestExceptionHandlerMessageEncoding(RQTestCase): From 261f4ac3d52f4456b8abfc70b35d52529b36319b Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 8 Sep 2017 10:30:25 +0100 Subject: [PATCH 2/2] Fixed #866 - Flak8 errors --- tests/test_worker.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/tests/test_worker.py b/tests/test_worker.py index 9c5f3e6..eb0ce05 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -471,22 +471,14 @@ class TestWorker(RQTestCase): logging work properly""" q = Queue("foo") w = Worker([q]) - q.enqueue( - 'tests.fixtures.say_hello', - name='阿达姆', - description='你好 世界!') - self.assertEqual( - w.work(burst=True), - True, - 'Expected at least some work done.') - q.enqueue( - 'tests.fixtures.say_hello_unicode', - name='阿达姆', - description='你好 世界!') - self.assertEqual( - w.work(burst=True), - True, - 'Expected at least some work done.') + q.enqueue('tests.fixtures.say_hello', name='阿达姆', + description='你好 世界!') + self.assertEqual(w.work(burst=True), True, + 'Expected at least some work done.') + q.enqueue('tests.fixtures.say_hello_unicode', name='阿达姆', + description='你好 世界!') + self.assertEqual(w.work(burst=True), True, + 'Expected at least some work done.') def test_suspend_worker_execution(self): """Test Pause Worker Execution"""