Skip to content

python: add support for defining interfaces - #8097

Merged
helderco merged 3 commits into
dagger:mainfrom
helderco:helder/dev-3249-support-interfaces-in-python
Apr 22, 2025
Merged

helderco merged 3 commits into
dagger:mainfrom
helderco:helder/dev-3249-support-interfaces-in-python

Conversation

@helderco

@helderco helderco commented Aug 3, 2024 •

Copy link
Copy Markdown
Contributor

This is a long time coming!

Closes #6213

What?

This adds support for defining Dagger interfaces in Dagger modules written in Python.

How?

To define an interface, create a Protocol class, decorated with @dagger.interface:

import typing
import dagger

@dagger.interface
class Duck(typing.Protocol):
    @dagger.function
    async def quack(self) -> str: ...

    @dagger.function
    def with_color(self, color: str) -> typing.Self: ...

Note that:

  • The functions in the interface need to be decorated with @dagger.function too.
  • Unless the return type is an object type, the function should be async to reflect the fact that the automatically generated client bindings for it will need to make an API request.
  • To refer to the current interface, use typing.Self.

Example

Here’s the same toy example in the original issue.

Pond module (no deps):

import typing
import dagger


@dagger.interface
class Duck(typing.Protocol):
    @dagger.function
    async def quack(self) -> str: ...


@dagger.object_type
class Pond:
    ducks: list[Duck] = dagger.field(default=list)

    @dagger.function
    def with_duck(self, duck: Duck) -> typing.Self:
        self.ducks.append(duck)
        return self

    @dagger.function
    async def quack_all(self) -> str:
        return "\n".join([await duck.quack() for duck in self.ducks])

Mallard module (no deps):

import dagger

@dagger.object_type
class Mallard:
    @dagger.function
    def quack(self) -> str:
        return "mallard quack"

Crested module (no deps):

import dagger

@dagger.object_type
class Crested:
    @dagger.function
    def quack(self) -> str:
        return "crested quack"

Caller module (deps on Mallard, Crested, and Pond):

import dagger
from dagger import dag

@dagger.object_type
class Caller:
    @dagger.function
    async def test(self) -> str:
        return await (
            dag.pond()
            .with_duck(dag.mallard().as_pond_duck())
            .with_duck(dag.crested().as_pond_duck())
            .quack_all()
        )

Future improvements

Support passing an implementation directly to an argument that expects an interface:

-.with_duck(dag.mallard().as_pond_duck())
+.with_duck(dag.mallard())

@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch from 4b09bb9 to 51f4162 Compare October 14, 2024 18:40
@github-actions

This comment was marked as resolved.

@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch from 51f4162 to da1bbb5 Compare October 30, 2024 00:05
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch 2 times, most recently from 18d58ff to 554fe8a Compare November 8, 2024 19:03
@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@github-actions github-actions Bot closed this Nov 30, 2024
@helderco helderco reopened this Dec 2, 2024
@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@github-actions github-actions Bot closed this Jan 18, 2025
@helderco helderco reopened this Jan 19, 2025
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch from 001bb25 to d07bcef Compare March 28, 2025 01:13
@helderco helderco changed the title feat(python): add support for defining interfaces python: add support for defining interfaces Mar 28, 2025
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch 2 times, most recently from 800d7be to 07d7c52 Compare April 15, 2025 15:27
@helderco
helderco requested review from TomChv and jedevc April 15, 2025 19:16
@helderco

Copy link
Copy Markdown
Contributor Author

This can be reviewed, just keeping in draft until #9538 is merged.

@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch 2 times, most recently from f47c672 to 2e92add Compare April 16, 2025 01:05
@helderco helderco added this to the v0.18.4 milestone Apr 16, 2025
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch 3 times, most recently from dbc8727 to 03ab103 Compare April 16, 2025 19:26
@helderco
helderco requested a review from sipsma April 18, 2025 14:27
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch 2 times, most recently from 2ac9552 to f017524 Compare April 20, 2025 23:07
@helderco

Copy link
Copy Markdown
Contributor Author

Marking as ready for review because the draft state is confusing.

@helderco
helderco marked this pull request as ready for review April 21, 2025 09:51
@helderco
helderco requested a review from a team as a code owner April 21, 2025 09:51
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch from f017524 to bfdc8d3 Compare April 21, 2025 11:25
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
@helderco
helderco force-pushed the helder/dev-3249-support-interfaces-in-python branch from bfdc8d3 to 2eadac5 Compare April 21, 2025 18:29

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦆

@helderco
helderco merged commit 224e3ab into dagger:main Apr 22, 2025
@helderco
helderco deleted the helder/dev-3249-support-interfaces-in-python branch April 22, 2025 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modules: Support for interfaces

3 participants