Deceivious
02/22/2023, 10:38 AMtask
decorator OR my knowledge of python decorators fails me 😅
While using the prefect task
decorators for some reason, the kwargs supplied while calling the decorated function are being converted into args.
To test this, Ive written a minimum viable script. <<Included in the thread>>
Expected use case: result when USE_PREFECT
is set to False.
Error use case: Error raised when USE_PREFECT
is set to True. If you look over the error trace log, when printing args and kargs. kargs is empty while the data expected in kargs is being sent to args.
ENV:
python: 3.10.8
prefect: 2.8.0import inspect
import functools
from prefect import flow, task
USE_PREFECT = True
def custom_task(function):
def wrapper(*args, **kwargs):
print("Task wrapper started")
ret = function(*args, **kwargs)
print("Task wrapper ended")
return ret
return wrapper
task_decorator = task if USE_PREFECT else custom_task
def do_something(function):
signature = inspect.signature(function)
@functools.wraps(function)
def wrapper(*args, **kwargs):
print(args, kwargs)
return function(**kwargs)
wrapper.__signature__ = signature
return wrapper
@task_decorator
@do_something
def normal_task(message: str, status_code: int):
print(f"{message=}")
print(f"{status_code=}")
@flow
def entry_flow():
normal_task(message="Hello", status_code=200)
if __name__ == "__main__":
entry_flow()