Anyone run into the issue where putting `@task` in...
# prefect-getting-started
c
Anyone run into the issue where putting
@task
in front of a class function where
self
is a parameter throws off the parameter passing? As in..
Copy code
class MyClass:
   def __init__():
      pass
   
   @task
   def my_func(self, arg1, arg2):
      pass

class_obj = MyClass()
class_obj('arg1', 'arg2')
Will throw an error where my_func is expecting arg2?
g
ran into this issue earlier, seems class method bindings using. a decorator is not directly supported.. worked around by writing a wrapper function that takes the object as an argument and invoking
<http://myclass.my|myclass.my>_func()
in the body, the other way could be to ditch classes and use functional code?
c
Ya, that makes sense. The wrapper is a bit of an annoying work-around but probably preferable in some of my pipelines where I use classes as a means of importing and managing configuration hyperparameters. Thanks for the input!
n
hi all! can you share what isn't working exactly and what version of prefect you are using? this should be supported in latest prefect
Copy code
In [1]: from prefect import task

In [2]: class MyClass:
   ...:     def __init__(self): pass
   ...:     @task
   ...:     def f(self, x, y):
   ...:         print(self, x, y)
   ...:

In [3]: obj = MyClass()

In [4]: obj.f(42, 9001)
10:30:29.413 | INFO    | Task run 'f' - Created task run 'f' for task 'f'
<__main__.MyClass object at 0x1160c4f20> 42 9001
10:30:29.423 | INFO    | Task run 'f' - Finished in state Completed()

In [5]: !prefect version
Version:             3.0.2+19.ga2dd7af098.dirty
API version:         0.8.4
Python version:      3.12.5
Git commit:          a2dd7af0
Built:               Tue, Sep 17, 2024 9:37 PM
OS/Arch:             darwin/arm64
Profile:             pong
Server type:         cloud
Pydantic version:    2.9.2
❤️ 1
ditch classes and use functional code?
this used to be our suggestion and honestly it's not a bad way to go IMO but we decided to explicitly support instance methods now, so it should work
g
@Nate this is great news! looking forward to try this out