Skip to content

Python Project Commands: Create thin wrapper around strawberry - #5300

Merged
helderco merged 14 commits into
dagger:mainfrom
helderco:helder/dag-1422-improve-python-project-command-dx
Jun 21, 2023
Merged

helderco merged 14 commits into
dagger:mainfrom
helderco:helder/dag-1422-improve-python-project-command-dx

Conversation

@helderco

@helderco helderco commented Jun 9, 2023 •

Copy link
Copy Markdown
Contributor

Follow-up on #5060 to improve DX in Python.

Using strawberry should be considered an implementation detail. This adds a thin layer on top of it because it's easier that way. There's still a lot to improve, but this aims to be a minimal experimental proof of concept.

I've placed demos in sdk/python/experimental because it's easier to run these from sdk/python. Otherwise it takes too long to load the context if run from the root of the repo.

Example

Simple cowsay example:

from typing import Annotated

import dagger
from dagger.server import command


@command
async def say(
    client: dagger.Client,
    msg: Annotated[str, "What do you want to say?"],
) -> str:
    """Say something with cowsay."""
    return await (
        client.container()
        .from_("python:alpine")
        .with_exec(["pip", "install", "cowsay"])
        .with_entrypoint(["cowsay"])
        .with_exec([msg])
        .stdout()
    )

Run from sdk/python:

❯ dagger do -c experimental/say say --msg "Simple is better than complex"

General usage notes

  • Top level commands are top-level functions decorated with @command.
  • Command groups are classes decorated with @commands and behave like a @dataclass for any defined attributes.
  • Function descriptions are set with the function’s docstring.
  • Argument descriptions are set with Annotated[<type>, "Description."].
  • A dagger client session is managed automatically, you just need to add a dagger.Client parameter. The parameter name doesn’t matter, it’s the type that’s important.

Implementation notes

  • I considered not requiring the decorators but I think this is more explicit and makes the implementation simpler;
  • I considered supporting a class-based top level command group, but it needs to be named Query and that doesn’t fit the CLI based mental model;
  • I considered decoupling the user’s definitions with strawberry’s by only producing the strawberry types at the last moment but that makes implementation more complex;

Packaging notes

Minimum required files:

  • main.py
  • dagger.json
  • pyproject.toml

Minimum pyproject.toml contents:

[project]
name = "example"
version = "0.0.0"
dependencies = [
    "dagger-io[server]"
]

Note that the examples in experimental have a bit more boilerplate to allow referencing a local path. We need this here to get the latest SDK code while developing this feature.

Remaining

Add more tests and clean up more things. However we can improve those in follow ups.

\cc @sipsma @cpdeethree

@helderco
helderco force-pushed the helder/dag-1422-improve-python-project-command-dx branch 3 times, most recently from 55447f8 to e704612 Compare June 13, 2023 14:26
@helderco
helderco force-pushed the helder/dag-1422-improve-python-project-command-dx branch from e704612 to a1e8d96 Compare June 14, 2023 10:10

@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.

Looks great so far! Like you said in discord, it would be good to add this to the integ test suite. I first added utils in the host export PR and have since done another iteration on them in the project CLI PR.

I'm hoping those can be merged in the next day or two, just waiting on reviews, but if something comes up and it takes a long time to merge them, I will split the integ test utils out to their own separate PR that can be merged quickly so you can just rebase on that.

Comment on lines 55 to 56

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.

Yeah you probably already saw it, but I added this to the go codegen to help out with what I think is the same (or similar-ish) problem, just in case that's helpful: https://github.com/dagger/dagger/pull/5157/files#diff-31244223d8a860c03288b53863173f3e261164aa5a30c6656810826206939c39R19

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep. 😄 I noticed you had the same kind of TODO comment after I added it. Planning on doing it in this PR, just had to focus on other things for 2 days. Also noticed your fix 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This hack does more than that though, it also does this:

dagger/sdk/go/server.go

Lines 812 to 840 in dc53110

// TODO: Add a .XXX_GraphQLObject(id) method to the generated SDK code to make this simpler + more maintainable
graphqlType := reflect.New(desiredType).Interface().(querybuilder.GraphQLMarshaller).XXX_GraphQLType()
switch graphqlType {
case "Container":
return client.Container(ContainerOpts{
ID: ContainerID(id),
}), nil
case "Directory":
return client.Directory(DirectoryOpts{
ID: DirectoryID(id),
}), nil
case "Socket":
return client.Socket(SocketOpts{
ID: SocketID(id),
}), nil
case "File":
return client.File(FileID(id)), nil
case "Secret":
return client.Secret(SecretID(id)), nil
case "Cache":
cacheID := CacheID(id)
return CacheVolume{
q: client.q,
c: client.c,
id: &cacheID,
}, nil
default:
return nil, fmt.Errorf("unhandled GraphQL marshaller type %s", graphqlType)
}

Comment thread sdk/python/src/dagger/server/_server.py Resolved
@helderco

Copy link
Copy Markdown
Contributor Author

@sipsma, yeah I saw those yesterday. I want to make a replica in python of the same commands in go/basic and parametrize the existing tests to loop over go and python along with git and local source.

@helderco
helderco force-pushed the helder/dag-1422-improve-python-project-command-dx branch 3 times, most recently from 993a3a0 to 1256a9b Compare June 20, 2023 15:34

@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.

Looks great, including all the updated integ tests. Like you said, there's more cleanup and tests we should add but that's true of go also, so we should feel free to merge this and iterate from there.

Comment thread core/pythonruntime.go Outdated

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.

general question, no need to address here, but is python generally backcompat in the 3.x range? Like will users typically be able to write code for python 3.9 and have it still work on 3.11? Or is fine grain version control a requirement?

Just wondering how soon we'll need to prioritize parameterizing this sort of thing (i.e. `"sdk": "python-3.11"). We'll absolutely need that eventually, just wondering if we can put it off or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The Python SDK only supports 3.10+. I took some liberties here that use 3.11+ features since we control the runtime image, but it's not a big change to back-support 3.10 if needed. I think we can put the image customization off for a while longer. If someone needs that customization I imagine it'll be more for other reasons rather than the Python version as this is independent from the version users need to run in production.

helderco added 14 commits June 21, 2023 09:05
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Just looking for a small performance improvement here.

Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
@helderco
helderco force-pushed the helder/dag-1422-improve-python-project-command-dx branch from 1256a9b to 0ddc1e2 Compare June 21, 2023 10:00
@helderco
helderco marked this pull request as ready for review June 21, 2023 10:00
Comment thread core/project.go
st.AddMount("/src", wdState, llb.Readonly) // TODO: not actually needed here, just makes go server code easier at moment
switch p.Config.SDK {
case "go", "python":
st.AddMount("/src", wdState, llb.Readonly)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sipsma I only added this to access client.host() in the commands, but otherwise not required. I know you have other plans to improve accessing the host.

@helderco
helderco merged commit 418e9e4 into dagger:main Jun 21, 2023
@helderco
helderco deleted the helder/dag-1422-improve-python-project-command-dx branch June 21, 2023 10:18
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.

2 participants