Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Celery Background Tasks: Property "Task" defined in "Celery" is read-only #4837

Closed
sr-verde opened this issue Oct 5, 2022 · 1 comment
Closed
Assignees
Labels

Comments

@sr-verde
Copy link

sr-verde commented Oct 5, 2022

Currently, the documentation says to replace celery.Task with a custom ContextTask. But this raises an error using mypy:

$ cat minimal.py 
from flask import Flask
from celery import Celery, Task

def make_celery(app: Flask):
    celery = Celery(app.import_name)
    celery.conf.update(app.config["CELERY_CONFIG"])

    class ContextTask(Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask
    return celery
$ mypy minimal.py
minimal.py:13: error: Property "Task" defined in "Celery" is read-only
minimal.py:13: error: Incompatible types in assignment (expression has type "Type[ContextTask]", variable has type "Task[Any]")

Instead one should initialize Celery with a string of the desired Task class. See Celery docs.

Invalid proposed solution
$ cat minimal.py 
from flask import Flask
from celery import Celery, Task


class ContextTask(Task):
    def __call__(self, *args, **kwargs):
        with app.app_context():
            return self.run(*args, **kwargs)


def make_celery(app: Flask):
    celery = Celery(app.import_name, task_cls="minimal.minimal:ContextTask")
    celery.conf.update(app.config["CELERY_CONFIG"])

    return celery
$ mypy minimal.py
Success: no issues found in 1 source file
@sr-verde sr-verde changed the title [DOC] Celery example: [DOC] Celery Background Tasks: Property "Task" defined in "Celery" is read-only Oct 5, 2022
@davidism davidism changed the title [DOC] Celery Background Tasks: Property "Task" defined in "Celery" is read-only Celery Background Tasks: Property "Task" defined in "Celery" is read-only Oct 5, 2022
@davidism davidism added the docs label Oct 5, 2022
@mavaras mavaras mentioned this issue Dec 5, 2022
6 tasks
@davidism davidism self-assigned this Jan 9, 2023
@davidism
Copy link
Member

The proposed fix isn't correct, it defeats the purpose of the Flask application factory function by referring to the app outside the function. That said, the current pattern isn't correct either if you're using the factory pattern for Flask as well.

However, I don't particularly care if MyPy doesn't think this is correct. MyPy isn't perfect, especially when it comes to completely valid Python patterns that large frameworks use but that MyPy hasn't figured out how support yet.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants