Prechádzať zdrojové kódy

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

Ask Solem 8 rokov pred
rodič
commit
879b93bd27

+ 2 - 1
celery/app/base.py

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

+ 5 - 1
celery/app/task.py

@@ -170,7 +170,8 @@ class Task(object):
     #: Enable argument checking.
     #: Enable argument checking.
     #: You can set this to false if you don't want the signature to be
     #: You can set this to false if you don't want the signature to be
     #: checked when calling the task.
     #: 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`,
     #: Maximum number of retries before giving up.  If set to :const:`None`,
     #: it will **never** stop retrying.
     #: it will **never** stop retrying.
@@ -311,6 +312,9 @@ class Task(object):
         conf = app.conf
         conf = app.conf
         cls._exec_options = None  # clear option cache
         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:
         for attr_name, config_name in cls.from_config:
             if getattr(cls, attr_name, None) is None:
             if getattr(cls, attr_name, None) is None:
                 setattr(cls, attr_name, conf[config_name])
                 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
 Make sure you are not affected by any of the important upgrade notes
 mentioned in the following section.
 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
 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
 This change also means that the ``abstract`` attribute of the task
 no longer has any effect.
 no longer has any effect.
 
 
+.. _v400-typing:
+
 Task argument checking
 Task argument checking
 ----------------------
 ----------------------
 
 
@@ -549,6 +554,13 @@ You can disable the argument checking for any task by setting its
     ... def add(x, y):
     ... def add(x, y):
     ...     return 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
 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, kw=2)
         add.delay(1, 2, foobar=3)
         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')
     @pytest.mark.usefixtures('depends_on_current_app')
     def test_unpickle_task(self):
     def test_unpickle_task(self):
         import pickle
         import pickle