<@ULVA73B9P> Can I create a deployment using an in...
# ask-marvin
s
@Marvin Can I create a deployment using an instance method decorate with
@flow
on a class?
m
thought for 307.6 seconds
Short answer: not directly. In Prefect 3.x, a deployment’s entrypoint must be an importable, top‑level callable that does not require an instance. An instance method (
def run(self, ...)
) decorated with
@flow
requires a
self
and Prefect won’t instantiate your class when loading the entrypoint, so it can’t be used as-is. Recommended patterns: 1) Wrap the method with a module-level flow (most reliable)
Copy code
from prefect import flow

class MyFlow:
    def __init__(self, cfg: str):
        self.cfg = cfg

    def run(self, x: int) -> int:
        # your logic here
        return x + 1

@flow
def run_my_flow(x: int) -> int:
    obj = MyFlow(cfg="prod")
    return obj.run(x)
Then deploy this entrypoint:
Copy code
prefect deploy ./path/to/file.py:run_my_flow
2) Make it a static/class method so it doesn’t need an instance (use with care) - For staticmethod, keep
@staticmethod
outside so the
@flow
sees a plain function at class creation time:
Copy code
class MyFlow:
    @staticmethod
    @flow
    def run(x: int) -> int:
        return x + 1
Entrypoint:
./path/to/file.py:MyFlow.run
Note: Avoid
@flow
on an instance method (
self
) — Python’s method binding won’t kick in after decoration, and Prefect cannot supply
self
. If you share a small snippet of how your class is structured, I can suggest the cleanest refactor for your case.