Repository: celery/celery

Issue:
celery amqp repl broken in celery 5.0.3+
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->
- [x] I have verified that the issue exists against the `master` branch of Celery.
- [x] This has already been asked to the [discussion group](https://groups.google.com/forum/#!forum/celery-users) first.
- [x] I have read the relevant section in the
  [contribution guide](http://docs.celeryproject.org/en/latest/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [x] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the bug was already fixed in the master branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [x] I have included the output of ``celery -A proj report`` in the issue.
    (if you are not able to do this, then at least specify the Celery
     version affected).
- [x] I have verified that the issue exists against the `master` branch of Celery.
- [x] I have included the contents of ``pip freeze`` in the issue.
- [x] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [ ] I have tried reproducing the issue on more than one Python version
  and/or implementation.
- [ ] I have tried reproducing the issue on more than one message broker and/or
  result backend.
- [ ] I have tried reproducing the issue on more than one version of the message
  broker and/or result backend.
- [ ] I have tried reproducing the issue on more than one operating system.
- [ ] I have tried reproducing the issue on more than one workers pool.
- [ ] I have tried reproducing the issue with autoscaling, retries,
  ETA/Countdown & rate limits disabled.
- [x] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- None

#### Possible Duplicates

- None

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**:
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:</b></summary>
<p>

```
software -> celery:5.0.5 (singularity) kombu:5.0.2 py:3.6.9
            billiard:3.6.4.0 py-amqp:5.0.6
platform -> system:Linux arch:64bit, ELF
            kernel version:4.15.0-140-generic imp:CPython
loader   -> celery.loaders.default.Loader
settings -> transport:amqp results:mongodb+srv://md_app_user:**@md-mongo.privatecircle.co/master_docs

accept_content: ['json']
broker_url: 'amqp://md_app_user:********@****************:5672//master_docs'
default_timezone: 'Asia/Kolkata'
imports: ['tasks']
result_backend: 'mongodb+srv://md_app_user:********@******************/master_docs'
result_serializer: 'json'
task_serializer: 'json'
timezone: 'Asia/Kolkata'
deprecated_settings: None
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
* **Minimal Python Version**: 3.6.9
* **Minimal Celery Version**: 5.0.3
* **Minimal Kombu Version**: 5.0.2
* **Minimal Broker Version**: RabbitMQ 3.8.11
* **Minimal Result Backend Version**: Mongo 4.4
* **Minimal OS and/or Kernel Version**: Ubuntu 18.04.5 (Linux kernel 4.15.0-140)
* **Minimal Broker Client Version**: N/A or Unknown
* **Minimal Result Backend Client Version**: N/A or Unknown

### Python Packages
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
amqp==5.0.6
backcall==0.2.0
billiard==3.6.4.0
boto3==1.17.51
botocore==1.20.51
cached-property==1.5.2
cchardet==2.1.7
celery==5.0.5
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
click-didyoumean==0.0.3
click-plugins==1.1.1
click-repl==0.1.6
decorator==5.0.6
dnspython==2.1.0
idna==2.10
importlib-metadata==3.10.0
ipython==7.16.1
ipython-genutils==0.2.0
jedi==0.17.2
jmespath==0.10.0
kombu==5.0.2
lxml==4.6.3
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.18
ptyprocess==0.7.0
Pygments==2.8.1
pymongo==3.11.3
python-dateutil==2.8.1
python-magic==0.4.22
pytz==2021.1
requests==2.25.1
s3transfer==0.3.6
six==1.15.0
traitlets==4.3.3
typing-extensions==3.7.4.3
urllib3==1.26.4
vine==5.0.0
wcwidth==0.2.5
zipp==3.4.1
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```python
# test.py
import celery
app = celery.Celery('proj')
```
```shell
$ celery -A test amqp repl
> exchange.declare

```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->
The AMQP interactive shell should accept this command and execute it and then prompt for the next command.

# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->
```
Traceback (most recent call last):
  File "/home/privatecircle/.virtualenvs/mca_document_manager/bin/celery", line 8, in <module>
    sys.exit(main())
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/celery/__main__.py", line 15, in main
    sys.exit(_main())
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/celery/bin/celery.py", line 213, in main
    return celery(auto_envvar_prefix="CELERY")
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click_repl/__init__.py", line 248, in repl
    group.invoke(ctx)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 1256, in invoke
    Command.invoke(self, ctx)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/privatecircle/.virtualenvs/mca_document_manager/lib/python3.6/site-packages/celery/bin/base.py", line 120, in caller
    app = ctx.obj.app
AttributeError: 'AMQPContext' object has no attribute 'app'
```



Discussion:
Hey @parthjoshi2007 :wave:,
Thank you for opening an issue. We will get back to you as soon as we can.
Also, check out our [Open Collective](https://opencollective.com/celery) and consider backing us - every little helps!

We also offer priority support for our sponsors.
If you require immediate assistance please consider sponsoring us.

you should try a more recent release on pypi as many cli bug was fixed in newer releases
or celery 5.1.0b1
I tried it on the latest code in the `master` branch as required for filing the bug report and the error is still there. The error was introduced in celery 5.0.3. For now, to get the shell working I've downgraded to 5.0.2

Resolving patch:
diff --git a/celery/bin/amqp.py b/celery/bin/amqp.py
--- a/celery/bin/amqp.py
+++ b/celery/bin/amqp.py
@@ -25,6 +25,10 @@ def __init__(self, cli_context):
         self.connection = self.cli_context.app.connection()
         self.channel = None
         self.reconnect()
+    
+    @property
+    def app(self):
+        return self.cli_context.app
 
     def respond(self, retval):
         if isinstance(retval, str):


Test patch: