Explorar el Código

App(strict_typing=False) now disables typing for all tasks.

Ask Solem hace 8 años
padre
commit
879b93bd27
Se han modificado 4 ficheros con 29 adiciones y 2 borrados
  1. 2 1
      celery/app/base.py
  2. 5 1
      celery/app/task.py
  3. 12 0
      docs/whatsnew-4.0.rst
  4. 10 0
      t/unit/tasks/test_tasks.py

+ 2 - 1
celery/app/base.py

@@ -233,7 +233,7 @@ class Celery(object):
                  amqp=None, events=None, log=None, control=None,
                  set_as_current=True, tasks=None, broker=None, include=None,
                  changes=None, config_source=None, fixups=None, task_cls=None,
-                 autofinalize=True, namespace=None, **kwargs):
+                 autofinalize=True, namespace=None, strict_typing=True, **kwargs):
         self.clock = LamportClock()
         self.main = main
         self.amqp_cls = amqp or self.amqp_cls
@@ -248,6 +248,7 @@ class Celery(object):
         self.steps = defaultdict(set)
         self.autofinalize = autofinalize
         self.namespace = namespace
+        self.strict_typing = strict_typing
 
         self.configured = False
         self._config_source = config_source

+ 5 - 1
celery/app/task.py

@@ -170,7 +170,8 @@ class Task(object):
     #: Enable argument checking.
     #: You can set this to false if you don't want the signature to be
     #: checked when calling the task.
-    typing = True
+    #: Defaults to :attr:`app.strict_typing <@Celery.strict_typing>`.
+    typing = None
 
     #: Maximum number of retries before giving up.  If set to :const:`None`,
     #: it will **never** stop retrying.
@@ -311,6 +312,9 @@ class Task(object):
         conf = app.conf
         cls._exec_options = None  # clear option cache
 
+        if cls.typing is None:
+            cls.typing = app.strict_typing
+
         for attr_name, config_name in cls.from_config:
             if getattr(cls, attr_name, None) is None:
                 setattr(cls, attr_name, conf[config_name])

+ 12 - 0
docs/whatsnew-4.0.rst

@@ -127,6 +127,9 @@ Step 3: Read the important notes in this document
 Make sure you are not affected by any of the important upgrade notes
 mentioned in the following section.
 
+An especially important note is that Celery now checks the arguments
+you send to a task by matching it to the signature (:ref:`v400-typing`).
+
 Step 4: Upgrade to Celery 4.0
 -----------------------------
 
@@ -516,6 +519,8 @@ general behavior, and then using the task decorator to realize the task:
 This change also means that the ``abstract`` attribute of the task
 no longer has any effect.
 
+.. _v400-typing:
+
 Task argument checking
 ----------------------
 
@@ -549,6 +554,13 @@ You can disable the argument checking for any task by setting its
     ... def add(x, y):
     ...     return x + y
 
+Or if you would like to disable this completely for all tasks
+you can pass ``strict_typing=False`` when creating the app:
+
+.. code-block::
+
+    app = Celery(..., strict_typing=False)
+
 Redis Events not backward compatible
 ------------------------------------
 

+ 10 - 0
t/unit/tasks/test_tasks.py

@@ -293,6 +293,16 @@ class test_tasks(TasksCase):
         add.delay(1, kw=2)
         add.delay(1, 2, foobar=3)
 
+    def test_typing__disabled_by_app(self):
+        with self.Celery(set_as_current=False, strict_typing=False) as app:
+            @app.task()
+            def add(x, y, kw=1):
+                pass
+            assert not add.typing
+            add.delay(1)
+            add.delay(1, kw=2)
+            add.delay(1, 2, foobar=3)
+
     @pytest.mark.usefixtures('depends_on_current_app')
     def test_unpickle_task(self):
         import pickle