<h1 dir="auto">Checklist</h1>

<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+label%3A%22Category%3A+Documentation%22+">issues list</a><br>
for similar or identical bug reports.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22Category%3A+Documentation%22">pull requests list</a><br>
for existing proposed fixes.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/commits/master">commit log</a><br>
to find out if the bug was already fixed in the master branch.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have included all related issues and possible duplicate issues in this issue<br>
(If there are none, check this box anyway).</li>
</ul>
<h2 dir="auto">Related Issues and Possible Duplicates</h2>

<h4 dir="auto">Related Issues</h4>
<ul dir="auto">
<li>None</li>
</ul>
<h4 dir="auto">Possible Duplicates</h4>
<ul dir="auto">
<li>None</li>
</ul>
<h1 dir="auto">Description</h1>

<p dir="auto">Per documentation here <a href="https://docs.celeryproject.org/en/stable/reference/celery.app.task.html#celery.app.task.Task.request" rel="nofollow"><code class="notranslate">Task.request</code></a>; expect this to be an instance of <a href="https://docs.celeryproject.org/en/stable/reference/celery.app.task.html#celery.app.task.Task.Request" rel="nofollow">Request</a>. However in reality this is an instance of <code class="notranslate">Context</code> that wraps <code class="notranslate">request._request_dict</code>. This lacks the <code class="notranslate">message</code> property as described on <a href="https://docs.celeryproject.org/en/stable/reference/celery.app.task.html#celery.app.task.Task.Request" rel="nofollow">Request</a>.</p>
<p dir="auto">My expectation per the documentation is that <code class="notranslate">Task.request</code> should behave as an instance of <code class="notranslate">Request</code>.</p>
<h1 dir="auto">Suggestions</h1>

<ul dir="auto">
<li>Document the Context class and then change <code class="notranslate">Task.request</code> to be an instance of Context; or</li>
<li>Include context missing attributes in <code class="notranslate">request._request_dict</code> so that it meets the <code class="notranslate">Request</code> expectations; or</li>
<li>Include the actual request instance in context.</li>
</ul>
<p dir="auto">In my usecase I ended up doing the following (see task_b):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from celery.worker.request import Request
from celery import Celery, Task
# celery_app is an instance of Celery

class MyRequest(Request):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._request_dict[&quot;message&quot;] = self.message

class MyTask(Task):
    Request = MyRequest

@celery_app.task()
def task_a(*args, **kwargs):
    print(task_a.request.message)  # Fails: AttributeError: 'Context' object has no attribute 'message'

@celery_app.task(base=MyTask)
def task_b(*args, **kwargs):
    print(task_b.request.message)  # Succeeds"><pre class="notranslate"><code class="notranslate">from celery.worker.request import Request
from celery import Celery, Task
# celery_app is an instance of Celery

class MyRequest(Request):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._request_dict["message"] = self.message

class MyTask(Task):
    Request = MyRequest

@celery_app.task()
def task_a(*args, **kwargs):
    print(task_a.request.message)  # Fails: AttributeError: 'Context' object has no attribute 'message'

@celery_app.task(base=MyTask)
def task_b(*args, **kwargs):
    print(task_b.request.message)  # Succeeds
</code></pre></div>
<h1 dir="auto">Checklist</h1>

<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+label%3A%22Category%3A+Documentation%22+">issues list</a><br>
for similar or identical bug reports.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22Category%3A+Documentation%22">pull requests list</a><br>
for existing proposed fixes.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/commits/master">commit log</a><br>
to find out if the bug was already fixed in the master branch.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have included all related issues and possible duplicate issues in this issue<br>
(If there are none, check this box anyway).</li>
</ul>
<h2 dir="auto">Related Issues and Possible Duplicates</h2>

<h4 dir="auto">Related Issues</h4>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="668682346" data-permission-text="Title is private" data-url="https://github.com/celery/celery/issues/6266" data-hovercard-type="issue" data-hovercard-url="/celery/celery/issues/6266/hovercard" href="https://github.com/celery/celery/issues/6266">#6266</a></li>
</ul>
<h4 dir="auto">Possible Duplicates</h4>
<ul dir="auto">
<li>None</li>
</ul>
<h1 dir="auto">Description</h1>

<p dir="auto">Since we migrated to Click, preload options are now using Click's API.<br>
Previously, the used argparse's API.</p>
<p dir="auto">This breaking change was not properly documented.</p>
<h1 dir="auto">Suggestions</h1>

<p dir="auto">We should update our <a href="https://github.com/celery/celery/blob/master/docs/userguide/extending.rst#command-line-programs">extending</a> user guide and use the correct APIs.</p>