Hi, is it posisble to add @flow to existing class ...
# ask-community
p
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
Hi! We generally don’t support this. You can do something like
Copy code
from prefect import flow


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


foo = Foo()
Foo.hi(foo, name="test")
p
Thanks how do we add class method a task?
Copy code
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
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…
Copy code
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
Got it thanks