Skip to content

Pass preload options to commands that do not accept them - #10837

Merged
auvipy merged 12 commits into
celery:mainfrom
Cherith1222:fix/7894-preload-plugin-commands
Oct 17, 2026
Merged

auvipy merged 12 commits into
celery:mainfrom
Cherith1222:fix/7894-preload-plugin-commands

Conversation

@Cherith1222

@Cherith1222 Cherith1222 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7894.

Issue #7894 adds a preload option such as --ini through user_options['preload']. celery -A example worker --ini app.ini works. celery -A example flower --ini app.ini raises TypeError: flower() got an unexpected keyword argument 'ini'. Celery adds those options to every subcommand, and Click passes them as keywords. Flower is a celery.commands entry point, and its callback does not accept them.

handle_preload_options now removes the preload names, sends user_preload_options, and then calls the original function. A subcommand that already carries preload options, and whose callback is not already wrapped, gets the same handling before Click calls it. A plugin callback that uses @click.pass_context is not given ctx again as a positional argument.

A command's own options, such as --force, are still passed through. A callback that already accepts **kwargs does not receive ini. The preload signal is sent once if that wrapper runs again.

On this commit (dd8f43a53a840bf78f83eb43e1f5b7e1f5852dff), Windows, Python 3.13.3: pytest t/unit/app/test_preload_cli.py t/unit/bin/test_upgrade.py -k "not test_preload_options" was 13 passed, 2 deselected. Those new tests failed with the TypeError before the change. Pre-commit on this diff passed, including isort and mypy. Full tox, bandit, apicheck, configcheck, integration tests, and a real Flower install were not run.

AI-assisted (Cursor).

Cherith reviewed this diff and confirmed it with the person who helped draft it.

Cherith1222 and others added 2 commits October 10, 2026 09:11
Celery adds those options to every sub-command, including celery.commands plugins such as Flower. Consume them and send user_preload_options before the callback runs.

AI-assisted (Cursor)
@auvipy
auvipy self-requested a review October 10, 2026 18:22
Comment thread Changelog.rst Resolved
@auvipy auvipy added this to the 5.7.0 milestone Oct 10, 2026
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.56%. Comparing base (2fa4aa2) to head (9389d8d).

Files with missing lines Patch % Lines
celery/bin/base.py 92.72% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10837   +/-   ##
=======================================
  Coverage   90.56%   90.56%           
=======================================
  Files         153      153           
  Lines       21367    21419   +52     
  Branches     2608     2621   +13     
=======================================
+ Hits        19350    19398   +48     
- Misses       1704     1707    +3     
- Partials      313      314    +1     
Flag Coverage Δ
unittests 90.54% <93.33%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this out and ran the real celery command group with a preload option (--ini) against main and this branch. One built-in command and one plugin-style command (a click command whose callback has no **kwargs, like Flower's):

Command main this PR
celery -A proj flowerlike --ini f.ini --port 7777 TypeError: flowerlike() got an unexpected keyword argument 'ini' works, --port still passed, user_preload_options sent
celery -A proj worker --ini w.ini: user_preload_options sent with {'ini': 'w.ini'} sent with {'ini': 'w.ini'}
celery -A proj worker --ini w.ini: ini in app.Worker(**kwargs) 'w.ini' missing

So the plugin crash in #7894 is fixed. The new tests fail on main and pass here.

Concern: built-in commands no longer receive preload values

handle_preload_options now pops the preload names before calling the command. For worker, beat and events, those values used to flow through **kwargs into app.Worker() / app.Beat(), and from there into custom bootsteps (def __init__(self, worker, ini=None, **kwargs)). The docs describe preload options as "passed to all sub-commands", and the signal is only the documented way to read them. Anyone who reads them in a bootstep would silently get None after this change.

Stripping only the values a callback cannot accept keeps the fix and avoids the break:

def _accepts_keyword(fun, name):
    try:
        params = inspect.signature(fun).parameters
    except (TypeError, ValueError):  # pragma: no cover
        return False
    return name in params or any(
        p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values())


def _take_preload_options(app, kwargs, fun=None):
    """Collect preload values, removing those the callback cannot accept."""
    taken = {}
    for name in _preload_option_names(app):
        if name in kwargs:
            if fun is not None and _accepts_keyword(fun, name):
                taken[name] = kwargs[name]
            else:
                taken[name] = kwargs.pop(name)
    return taken

with _take_preload_options(app, kwargs, f) in handle_preload_options and _take_preload_options(app, kwargs, fun) in consume_preload_options. With that:

  • worker gets ini again;
  • the Flower-like command still works;
  • a decorated command without **kwargs still works;
  • the signal is still sent once.

Of the new tests, only test_var_keyword_callback_receives_preload_options_only_via_signal would need to change, since it asserts the behaviour above.

Minor

command.params.extend(preload_options) runs on every invocation of the celery group, so in a long-lived process (or a test session) the same options are appended again each time. That was already the case before this PR. The new wrapping is guarded by _handles_preload_options, so it does not get worse, but a membership check before extend would be cheap to add while touching this code.

worker, beat, and events take those values in kwargs. Only a callback that would raise TypeError loses the name, and the signal still receives every preload value.

AI-assisted (Cursor).
@Cherith1222

Copy link
Copy Markdown
Contributor Author

Preload values are still sent on user_preload_options. A name is removed from the callback kwargs only when that callback cannot accept it.

worker, beat, and events take **kwargs, so they keep the preload values. A callback with neither **kwargs nor a matching parameter still does not raise TypeError. The test that expected a **kwargs callback to drop those values now expects it to keep them, and another test keeps an explicit parameter while dropping the other preload name.

Drafted with Cursor.

@auvipy
auvipy self-requested a review October 13, 2026 17:19

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked at 41e908e, following up on my earlier review.

I ran the real celery command group with the pyramid_celery_app test app (--ini / --ini-var as preload options) on main and on this branch. flowerlike is a plugin-style command whose callback has no **kwargs, like Flower's, plus its own --port option:

                                    main                                     this PR
flowerlike --ini f.ini --port 7777  TypeError: unexpected keyword 'ini'      runs, port=7777, signal {'ini': 'f.ini', ...}
worker --ini w.ini                  signal sent, Worker(ini='w.ini')         signal sent, Worker(ini='w.ini')
purge -f --ini p.ini                signal sent once                         signal sent once
report --ini r.ini                  no signal                                signal sent once
second invoke in the same process   TypeError                                signal sent once (wrapper not stacked)
  • My earlier concern is resolved. worker, beat and events take **kwargs, so they keep the preload values and app.Worker() gets ini again. Only a callback that can't accept a name loses it, and that callback still receives the value through user_preload_options.
  • The crash from #7894 is fixed. The plugin's own options are passed through unchanged, and built-ins already decorated with handle_preload_options aren't wrapped a second time, so the signal is sent once.
  • t/unit/app/test_preload_cli.py and t/unit/bin/ pass locally (61 passed), and CI is green.
  • The Changelog.rst change was removed as requested; the diff now touches only celery/bin/base.py, celery/bin/celery.py and the tests.

Remaining suggestions (non-blocking)

  1. Built-in commands that never used handle_preload_options, such as report, now send user_preload_options too, because consume_preload_options wraps every command in the group. That seems like the right behaviour (preload options are documented as applying to all sub-commands), but it is a visible change for receivers, so it belongs in the docs note below.
  2. On the earlier request for documentation: the "Preload options" section in docs/userguide/extending.rst is the natural place for a short .. versionchanged:: 5.7 note. It should say that preload options now work with commands installed through the celery.commands entry point (e.g. Flower), that a callback which can't accept an option receives it only through user_preload_options, and that the signal is now sent for every sub-command.

@auvipy

auvipy commented Oct 17, 2026

Copy link
Copy Markdown
Member
  • Built-in commands that never used handle_preload_options, such as report, now send user_preload_options too, because consume_preload_options wraps every command in the group. That seems like the right behaviour (preload options are documented as applying to all sub-commands), but it is a visible change for receivers, so it belongs in the docs note below.
  • On the earlier request for documentation: the "Preload options" section in docs/userguide/extending.rst is the natural place for a short .. versionchanged:: 5.7 note. It should say that preload options now work with commands installed through the celery.commands entry point (e.g. Flower), that a callback which can't accept an option receives it only through user_preload_options, and that the signal is now sent for every sub-command.

@Cherith1222 please recheck this two

report never used handle_preload_options, but the umbrella command wraps every callback, so the signal is sent there too. The 5.7 note records that this is intentional.

AI-assisted (Cursor).
@Cherith1222

Copy link
Copy Markdown
Contributor Author

Rechecked both points on this branch.

report is not decorated with handle_preload_options. celery -A t.unit.bin.proj.pyramid_celery_app report --ini some_ini.ini still exits 0 and sends user_preload_options with ini=some_ini.ini. The same preload option on worker sends that signal too. consume_preload_options wraps every command in the group, so built-in commands that never used the decorator now send user_preload_options. That is the intended behavior.

docs/userguide/extending.rst, section "Preload options", now has a .. versionchanged:: 5.7 note. It says preload options work with commands installed through the celery.commands entry point (e.g. Flower), a callback that cannot accept an option receives it only through user_preload_options, and the signal is sent for every sub-command, including report.

Drafted with Cursor.

@auvipy
auvipy merged commit be75e2c into celery:main Oct 17, 2026
485 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Celery not configuring flower subcommand for user-defined preload CLI options

3 participants