https://prefect.io logo
Title
p

Praveen Shilavantar

12/19/2022, 10:03 PM
Hi, is it posisble to add @flow to existing class methods?
class API:
@flow
def start(self):
pass
I am getting TypeError: missing a required argument: 'self' (edited)
1
z

Zanie

12/19/2022, 10:09 PM
Hi! We generally don’t support this. You can do something like
from prefect import flow


class Foo:
    @flow
    def hi(self, name):
        print("hi", name)


foo = Foo()
Foo.hi(foo, name="test")
p

Praveen Shilavantar

12/19/2022, 10:14 PM
Thanks how do we add class method a task?
class Foo:
    @task
    def greetings(self, name):
        print("Hello", name)
    
    @flow
    def hi(self, name):
        print("hi", name)
        self.greetings(name)


foo = Foo()
Foo.hi(foo, name="test")
This not working..
Is it not a blocker for people who already coded using class methods in existing projects? how it is supported?
z

Zanie

12/19/2022, 10:24 PM
We don’t recommend using class methods because statefulness is not guaranteed
For example, if your task is submitted to a worker when it mutates
self.some_attribute
that state will often not be propagated.
A while ago, a user opened a pull request to just immediately error when sometime tries to decorate a class method: https://github.com/PrefectHQ/prefect/pull/6864
fwiw from your example…
from prefect import flow, task


class Foo:
    @flow
    def hi(self, name):
        print("hi", name)
        Foo.bye(self)

    @task
    def bye(self):
        print("bye")


foo = Foo()
Foo.hi(foo, name="test")
works
👍 1
I doubt we will add first-class support here anytime soon as it’s going to cause more confusing behavior than it’s worth.
p

Praveen Shilavantar

12/19/2022, 10:46 PM
Got it thanks