Add a small hint when tasks are called with missing keywords
Calling swh scheduler task add load-git https://example.org/repository.git
instead of swh scheduler task add load-git url=https://example.org/repository.git
will result in a confusing TypeError being raised by the scheduler.
This change adds a small note to the TypeError pointing at the
arguments of swh scheduler task add
.
This uses the BaseException.add_note
method which was introduced in
Python 3.11.
Sadly, I was not able to understand where to plug a test for this.
The result looks like:
ERROR celery.app.trace:trace.py:270 Task swh.scheduler.tests.tasks.keyword[c6764342-bdda-4d92-9e2d-11e6119ae49b] raised unexpected: TypeError("keyword() missing 1 required keyword-only argument: 'keyword'")
Traceback (most recent call last):
File "/home/lunar/.pyenv/versions/swh3.11/lib/python3.11/site-packages/celery/app/trace.py", line 477, in trace_task
R = retval = fun(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^
File "/home/lunar/Documents/SWH/swh-environment/swh-scheduler/build/__editable__.swh.scheduler-1.13.1.dev2+g869514d.d20231211-py3-none-any/swh/scheduler/task.py", line 67, in __call__
raise e
File "/home/lunar/Documents/SWH/swh-environment/swh-scheduler/build/__editable__.swh.scheduler-1.13.1.dev2+g869514d.d20231211-py3-none-any/swh/scheduler/task.py", line 62, in __call__
result = super().__call__(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lunar/.pyenv/versions/swh3.11/lib/python3.11/site-packages/celery/app/task.py", line 411, in __call__
return self.run(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: keyword() missing 1 required keyword-only argument: 'keyword'
Would you have forgotten to write the keywords when calling `swh scheduler task add`?
Merge request reports
Activity
assigned to @lunar
Jenkins job DSCH/gitlab-builds #148 succeeded .
See Console Output and Coverage Report for more details.We still support (and run code on) python versions that are lower than 3.11, so this should be made optional.
I'm not sure all
TypeError
s caught at that stage will be caused by the arguments of the task.The message feels a bit... patronizing, for lack of a better word?
I would prefer something neutral like "Check the arguments passed to `swh scheduler task add`."
58 58 self.statsd.increment("swh_task_called_count") 59 59 self.statsd.gauge("swh_task_start_ts", ts()) 60 60 with self.statsd.timed("swh_task_duration_seconds"): 61 try: 61 62 result = super().__call__(*args, **kwargs) 63 except TypeError as e: 64 e.add_note( 65 "Would you have forgotten to write the keywords when calling " 66 "`swh scheduler task add`?" @lunar Now that we're using 3.10 in production, maybe the optional part @olasd mention is no longer useful (I don't know if the "lower" is strict or not in his sentence ;).
I've iterated over olasd's suggestion as an inline suggested change in the code. Note that i've just added the "keyword" prefix.
Almost ready to be merged if you wanna have an extra push