Skip to content

Commit d3e5df3

Browse files
authored
docs: remove obsolete section "Automatic naming and relative imports" (celery#6904)
Celery 5.0 dropped support for Python 2 and only supports Python 3. Since Python 3 does not support old-style relative imports, the entire section can be dropped. Also remove a reference to the section above in docs/django/first-steps-with-django.rst. This change shall *not* be backported to Celery <5.0. Fixes celery#6903. Signed-off-by: Jinoh Kang <jinoh.kang.kr@gmail.com>
1 parent 3cf5072 commit d3e5df3

File tree

2 files changed

+0
-95
lines changed

2 files changed

+0
-95
lines changed

docs/django/first-steps-with-django.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,6 @@ concrete app instance:
153153
You can find the full source code for the Django example project at:
154154
https://github.com/celery/celery/tree/master/examples/django/
155155

156-
.. admonition:: Relative Imports
157-
158-
You have to be consistent in how you import the task module.
159-
For example, if you have ``project.app`` in ``INSTALLED_APPS``, then you
160-
must also import the tasks ``from project.app`` or else the names
161-
of the tasks will end up being different.
162-
163-
See :ref:`task-naming-relative-imports`
164-
165156
Extensions
166157
==========
167158

docs/userguide/tasks.rst

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -237,92 +237,6 @@ named :file:`tasks.py`:
237237
>>> add.name
238238
'tasks.add'
239239
240-
.. _task-naming-relative-imports:
241-
242-
Automatic naming and relative imports
243-
-------------------------------------
244-
245-
.. sidebar:: Absolute Imports
246-
247-
The best practice for developers targeting Python 2 is to add the
248-
following to the top of **every module**:
249-
250-
.. code-block:: python
251-
252-
from __future__ import absolute_import
253-
254-
This will force you to always use absolute imports so you will
255-
never have any problems with tasks using relative names.
256-
257-
Absolute imports are the default in Python 3 so you don't need this
258-
if you target that version.
259-
260-
Relative imports and automatic name generation don't go well together,
261-
so if you're using relative imports you should set the name explicitly.
262-
263-
For example if the client imports the module ``"myapp.tasks"``
264-
as ``".tasks"``, and the worker imports the module as ``"myapp.tasks"``,
265-
the generated names won't match and an :exc:`~@NotRegistered` error will
266-
be raised by the worker.
267-
268-
This is also the case when using Django and using ``project.myapp``-style
269-
naming in ``INSTALLED_APPS``:
270-
271-
.. code-block:: python
272-
273-
INSTALLED_APPS = ['project.myapp']
274-
275-
If you install the app under the name ``project.myapp`` then the
276-
tasks module will be imported as ``project.myapp.tasks``,
277-
so you must make sure you always import the tasks using the same name:
278-
279-
.. code-block:: pycon
280-
281-
>>> from project.myapp.tasks import mytask # << GOOD
282-
283-
>>> from myapp.tasks import mytask # << BAD!!!
284-
285-
The second example will cause the task to be named differently
286-
since the worker and the client imports the modules under different names:
287-
288-
.. code-block:: pycon
289-
290-
>>> from project.myapp.tasks import mytask
291-
>>> mytask.name
292-
'project.myapp.tasks.mytask'
293-
294-
>>> from myapp.tasks import mytask
295-
>>> mytask.name
296-
'myapp.tasks.mytask'
297-
298-
For this reason you must be consistent in how you
299-
import modules, and that is also a Python best practice.
300-
301-
Similarly, you shouldn't use old-style relative imports:
302-
303-
.. code-block:: python
304-
305-
from module import foo # BAD!
306-
307-
from proj.module import foo # GOOD!
308-
309-
New-style relative imports are fine and can be used:
310-
311-
.. code-block:: python
312-
313-
from .module import foo # GOOD!
314-
315-
If you want to use Celery with a project already using these patterns
316-
extensively and you don't have the time to refactor the existing code
317-
then you can consider specifying the names explicitly instead of relying
318-
on the automatic naming:
319-
320-
.. code-block:: python
321-
322-
@app.task(name='proj.tasks.add')
323-
def add(x, y):
324-
return x + y
325-
326240
.. _task-name-generator-info:
327241

328242
Changing the automatic naming behavior

0 commit comments

Comments
 (0)