Skip to main content

Name Overrides

Sometimes it's possible to get into a situation where the name we want to use for the Dagger API is reserved or has a naming conflict within Python.

To resolve this, the @dagger.function decorator and the dagger.field descriptor have a name argument to provide an alternative name for the Dagger API. And for function arguments, annotate with an additional dagger.Arg metadata.

Here's an example with a renamed object attribute (and constructor argument), function and function argument:

"""Example with name overrides"""
from typing import Annotated

import dagger
from dagger import Arg, Doc, dag, field, function, object_type


@object_type
class MyModule:
def_: Annotated[str, Doc("Definition")] = field(name="def", default="latest")

@function(name="import")
def import_(
self,
from_: Annotated[str, Arg(name="from"), Doc("Image ref")] = "alpine",
) -> dagger.Container:
"""Import the specified image"""
return dag.container().with_label("definition", self.def_).from_(from_)

Confirm with dagger call --help that the names were overridden:

Flags:
--def string Definition (default "latest")

Function Commands:
def Definition
import Import the specified image

And the same for the function argument, with dagger call import --help:

Flags:
--from string Image ref (default "alpine")