Skip to content

Python SDK dev module - #6117

Merged
helderco merged 7 commits into
dagger:mainfrom
helderco:sdk/python/dev-module
Dec 14, 2023
Merged

helderco merged 7 commits into
dagger:mainfrom
helderco:sdk/python/dev-module

Conversation

@helderco

@helderco helderco commented Nov 16, 2023 •

Copy link
Copy Markdown
Contributor

Summary

This change adds a module for developing the Python SDK.

Motivation

Main purpose is to allow others to run Python tasks without having to have a Python environment in their computers. This can be done in phases but includes something from all of the following:

  1. Migrate hatch tasks;
  2. Add utilities for testing or linting any Python files (e.g., doc snippets);
  3. Migrate CI tasks (from mage);

The last one is related to a backlog item for using zenith in our CI. The ./ci module will use ./sdk/python/dev as a dependency.

Interesting patterns

I found the following patterns interesting while making this module.

Project structure

It’s a good example of how to structure bigger projects into multiple files. Instead of a single src/main.py file, you can turn it into a src/main package, with __init__.py as the “entrypoint” and other files imported from there.

Constructors

With latest constructor support in #6152 it’s also possible to re-use an object’s constructor in another object’s method or top level function.

So the following lines allow breaking into dedicated objects various aspects of development, and provide top-level functions to instantiate those objects, without duplication:

https://github.com/dagger/dagger/blob/3f0163be8d8565e449ce8c7bb0903e0b0802f435/sdk/python/dev/src/main/__init__.py#L26-L30

Laziness

The object types were created with modularity principles in mind, and maximum laziness. Especially in the Deps object, there’s no special care to resolve things before using. By passing around lazy objects like any programing object, the engine resolves the DAG more efficiently.

https://github.com/dagger/dagger/blob/3f0163be8d8565e449ce8c7bb0903e0b0802f435/sdk/python/dev/src/main/deps.py#L219-L225

I was surprised however at how little async calls I actually needed in the end seeing how much this module actually does. One is a joke1, the other is an experiment and the last one was needed to get the an endpoint value for a test condition (Test.provision) that’s hardcoded right now in a GHA workflow that’s only run when PR’s are merged into main. I tested the condition locally on macOS and it failed in Linux on CI. This test reproduces that environment with docker so anyone can test it locally.

Concurrency

The experiment is between dagger call all-tests (lazy) and dagger-call tests (concurrent). One uses concurrency to test the SDK with multiple Python versions, while the other just returns a list of lazy Test objects for each version.

https://github.com/dagger/dagger/blob/3f0163be8d8565e449ce8c7bb0903e0b0802f435/sdk/python/dev/src/main/__init__.py#L98

What’s interesting is that returning a list of functions seems viable to replace the need for concurrency at the language level in some cases.

If you run dagger call tests it’s fixed to executing Test.default, which runs all tests. If you just want to run unit tests you’re out of luck if you want a single command. But with dagger call all-tests unit, you’re basically selecting a field for each item the the list so there’s more freedom to sub-select a function on multiple objects, while executing them concurrently and without having to do any concurrency at the language level.

with_focus and pipeline

If you rely on dagger call adding .sdout() to a Container, then you’ll need the let it finish to see the test run’s results, unless you have --focus=false. But if you take care in adding .with_focus() before the execs that you’re interesting in seeing the output of, then it’ll stream that output in the TUI during execution in the default --focus=true, without showing the clutter.

https://github.com/dagger/dagger/blob/3f0163be8d8565e449ce8c7bb0903e0b0802f435/sdk/python/dev/src/main/test.py#L56-L61

This is especially nice when you’re returning multiple lazy Test objects for different Python versions and seeing their output in real-time, concurrently.

There’s a catch 22 problem however. On one hand, the automatic addition of .stdout by dagger call creates duplicated output (one from the focused step, the other from the final response). On the other, if you don’t add stdout you wouldn’t see anything without the TUI (e.g., --silent).

There’s several ways this could be fixed but I think relying more on with_focus provides the better experience. Paired with pipeline you can group the output in the TUI with your own labels, while with stdout from multiple objects you get all the output but they’re not categorized or grouped. Just printed one after the other, in the ordered the happened to arrive.

What’s next?

  • Directories are only filtered when using the default sources (from host). If you provide an alternative dir via the CLI, that’s not filtered. Try to exclude files from any Directory instead.
  • Port more functions from mage and use as dependency of a ./ci module in Go, to delegate all Python related tasks to, for example, linting doc snippets. Related to:
  • Document how to:
    • test a doc snippet using the module
    • lint doc snippets

Footnotes

  1. dagger -m dev call joke: "['hip', 'hip'] (hip hip array!)" ↩

@helderco
helderco force-pushed the sdk/python/dev-module branch from 60bc4c9 to 2f84ca9 Compare November 20, 2023 17:00
@helderco
helderco force-pushed the sdk/python/dev-module branch from 2f84ca9 to 205ffbc Compare November 21, 2023 18:40
@gerhard gerhard mentioned this pull request Nov 28, 2023
@helderco
helderco force-pushed the sdk/python/dev-module branch from 205ffbc to 3f0163b Compare November 28, 2023 23:47
@helderco
helderco marked this pull request as ready for review November 28, 2023 23:47
@helderco
helderco requested a review from sipsma November 28, 2023 23:47
@helderco

Copy link
Copy Markdown
Contributor Author

Ready for review. Updated the description with more context.

@helderco

helderco commented Dec 7, 2023

Copy link
Copy Markdown
Contributor Author

Rebased.

Note for reviewer. While there's some functions that don't work quite right from outside of the sdk/phython dir, like linting docs, the module does what was intended initially: to replace hatch tasks with dagger. CI with mage works as usual, replacing it is part of a larger effort and out of scope for this PR.

Easier to make improvements over time in followups because while this isn't merged it takes effort to use it in other branches.

@gerhard

gerhard commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

Starting to look into this one now.

@gerhard

gerhard commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

Am I doing something wrong here?

image

@helderco

helderco commented Dec 7, 2023

Copy link
Copy Markdown
Contributor Author

@gerhard you may need hack/dev if you haven't built it. It depends on main.

@gerhard

gerhard commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

Thanks makes sense! Will try again with a dev Engine & CLI.

With constructor support it's not possible to create a function from an object's constructor.

Also added more documentation.

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]>
@helderco
helderco force-pushed the sdk/python/dev-module branch from 1ec1458 to ef0b0e0 Compare December 12, 2023 16:07

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

This is really really really cool, so much neat stuff. The idea of returning a list of Test objects and then selecting a container from them to run a specific subtest is super interesting, seems like a very re-usable general idea!


The only thing I noticed is that I'm currently getting this when I run ../../hack/dev dagger call -m dev tests

+ exec dagger call -m dev tests
✔ dagger call tests [48.49s]
┃ true                                                                                                                                                                                                                                                  
✘ sync ERROR [0.01s]
✘ exec foobar ERROR [0.13s]
┃ panic: exec: "foobar": executable file not found in $PATH goroutine 1 [running, locked to thread]:                                                                                                                                                    
┃ runtime/debug.Stack()                                                                                                                                                                                                                                 
┃         /usr/local/go/src/runtime/debug/stack.go:24 +0x64                                                                                                                                                                                             
┃ main.main.func1()                                                                                                                                                                                                                                     
┃         /app/cmd/shim/main.go:63 +0x34                                                                                                                                                                                                                
┃ panic({0xa7dae0?, 0x40005d8b80?})                                                                                                                                                                                                                     
┃         /usr/local/go/src/runtime/panic.go:914 +0x218                                                                                                                                                                                                 
┃ main.shim()                                                                                                                                                                                                                                           
┃         /app/cmd/shim/main.go:291 +0xda8                                                                                                                                                                                                              
┃ main.main()                                                                                                                                                                                                                                           
┃         /app/cmd/shim/main.go:76 +0x98                                                                                                                                                                                                                
✘ directory(path: "foobar") ERROR [0.02s]

However, quite mysteriously I hit that, had to go look at something else for a few hours and then when I came back and ran it again it passed. So could be a flake? Just worth double checking


docker_version = "24.0.7"

ctr = dag.dockerd().attach(

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.

Nice! Can't wait to migrate all the provisioning tests to this approach 😃

This publishes a cli binary in an ephemeral http server and checks
if the SDK can download, extract and run it.
"""
# TODO: Most of this setup can be reused for all SDKs that need to

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 commented Dec 13, 2023 •

Copy link
Copy Markdown
Contributor Author

The only thing I noticed is that I'm currently getting this when I run ../../hack/dev dagger call -m dev tests

+ exec dagger call -m dev tests
✔ dagger call tests [48.49s]
┃ true                                                                                                                                                                                                                                                  
✘ sync ERROR [0.01s]
✘ exec foobar ERROR [0.13s]
┃ panic: exec: "foobar": executable file not found in $PATH goroutine 1 [running, locked to thread]:                                                                                                                                                    
┃ runtime/debug.Stack()                                                                                                                                                                                                                                 
┃         /usr/local/go/src/runtime/debug/stack.go:24 +0x64                                                                                                                                                                                             
┃ main.main.func1()                                                                                                                                                                                                                                     
┃         /app/cmd/shim/main.go:63 +0x34                                                                                                                                                                                                                
┃ panic({0xa7dae0?, 0x40005d8b80?})                                                                                                                                                                                                                     
┃         /usr/local/go/src/runtime/panic.go:914 +0x218                                                                                                                                                                                                 
┃ main.shim()                                                                                                                                                                                                                                           
┃         /app/cmd/shim/main.go:291 +0xda8                                                                                                                                                                                                              
┃ main.main()                                                                                                                                                                                                                                           
┃         /app/cmd/shim/main.go:76 +0x98                                                                                                                                                                                                                
✘ directory(path: "foobar") ERROR [0.02s]

However, quite mysteriously I hit that, had to go look at something else for a few hours and then when I came back and ran it again it passed. So could be a flake? Just worth double checking

That's a known issue actually! It's harmless but we should fix it I think. I forgot to create an issue.

There's a few integration tests around .sync that trigger errors on purpose because .sync behavior is harder to test on a successful case.

Screenshot 2023-12-13 at 10 09 44

@sipsma So the Go panic is being raised on non-existing withExec command and directory path. I haven't looked into shim/main.go yet.

It's harmless because the error is correctly raised, captured and asserted from the SDK's side (and thus the tests pass), but I didn't expect there to be a panic in the logs.

@helderco
helderco merged commit a06c4a0 into dagger:main Dec 14, 2023
@helderco
helderco deleted the sdk/python/dev-module branch December 14, 2023 12:02
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.

3 participants