MCPcopy
hub / github.com/celery/celery / wrapper

Function wrapper

celery/app/base.py:158–199  ·  view source on GitHub ↗
(*task_args, **task_kwargs)

Source from the content-addressed store, hash-verified

156
157 @functools.wraps(task_fun)
158 def wrapper(*task_args, **task_kwargs):
159 # Validate task parameters if type hinted as BaseModel
160 bound_args = task_signature.bind(*task_args, **task_kwargs)
161 for arg_name, arg_value in bound_args.arguments.items():
162 if type_hints and arg_name in type_hints:
163 arg_annotation = type_hints[arg_name]
164 else:
165 arg_annotation = task_signature.parameters[arg_name].annotation
166
167 optional_arg = get_optional_arg(arg_annotation)
168 if optional_arg is not None and arg_value is not None:
169 arg_annotation = optional_arg
170
171 if annotation_issubclass(arg_annotation, BaseModel):
172 bound_args.arguments[arg_name] = arg_annotation.model_validate(
173 arg_value,
174 strict=strict,
175 context={**context, 'celery_app': app, 'celery_task_name': task_name},
176 )
177
178 # Call the task with (potentially) converted arguments
179 returned_value = task_fun(*bound_args.args, **bound_args.kwargs)
180
181 # Dump Pydantic model if the returned value is an instance of pydantic.BaseModel *and* its
182 # class matches the typehint
183 if type_hints and 'return' in type_hints:
184 return_annotation = type_hints['return']
185 else:
186 return_annotation = task_signature.return_annotation
187
188 optional_return_annotation = get_optional_arg(return_annotation)
189 if optional_return_annotation is not None:
190 return_annotation = optional_return_annotation
191
192 if (
193 annotation_is_class(return_annotation)
194 and isinstance(returned_value, BaseModel)
195 and isinstance(returned_value, return_annotation)
196 ):
197 return returned_value.model_dump(**dump_kwargs)
198
199 return returned_value
200
201 return wrapper
202

Callers

nothing calls this directly

Calls 5

get_optional_argFunction · 0.85
annotation_issubclassFunction · 0.85
annotation_is_classFunction · 0.85
bindMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected