Skip to content

feat(python): allow module package names other than main - #8709

Merged
sipsma merged 6 commits into
dagger:mainfrom
helderco:helder/dev-4565-python-allow-module-package-names-other-than-main
Nov 7, 2024
Merged

sipsma merged 6 commits into
dagger:mainfrom
helderco:helder/dev-4565-python-allow-module-package-names-other-than-main

Conversation

@helderco

@helderco helderco commented Oct 14, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #8535

Note

This is a backwards compatible change. Only new modules get the new file structure. For existing modules, there's more freedom to change, but not enforced.

What?

This changes the layout for new modules. For example:

❯ dagger init --sdk=python my-module

Generates the name my-module for the project:

[project]
-name = "main"
+name = "my-module"

And puts sources under src/my_module:

.
 ├── sdk
 ├── src
-│  └── main
+│  └── my_module
 │     ├── __init__.py
+│     └── main.py
 ├── dagger.json
 ├── pyproject.toml
 └── uv.lock

With __init__.py using this import:

from .main import MyModule as MyModule

But users have now complete control to specify where the main object needs to be imported from. For example:

# pyproject.toml

[project.entry-points."dagger.mod"]
main_object = "my_module.main:MyModule"

With the above, the __init__.py could be empty as it no longer needs to import the main object there because the entry point is specifically importing from my_module.main, not my_module.

Refactors

Simpler loading of dagger.mod.Module

Previously, user code was imported by an hardcoded:

import main

The SDK would look for instances of dagger.mod.Module, which is what’s actually needed to execute. So you could use a custom subclass to customize some aspect of module handling. One early example of what I had in mind for this is to allow registering more hooks to teach dagger how to convert some custom types:

import dagger.mod

class CustomModule(dagger.mod.Module)
    def __call__(self):
        # customize initialization
        ...

mod = CustomModule()

@mod.object_type
class Foo:
    ...

To do this, I added the dagger.mod.Module to the @object_type decorated class. This way, the SDK only needs to import the main object itself, since it’s now able to get the corresponding dagger.mod.Module instance from there. So even if you have different instances of dagger.mod.Module for some reason, it doesn’t matter.

This is more correct, and avoids the need for the extra “smartness” and validation.

Added customization on where to import the main object

I could just replace the hardcoded main:<MainObject> by the normalized module name coming from an env variable (e.g., my_module:<MainObject>), but I wanted to take advantage of the new file structure and module loading simplification, to give users more control by being able to specify explicitly where the main object should be imported from.

This way users have even less restrictions on how to organize their files. For example, it’s now possible to even remove the import in src/my_module/__init__.py:

-from .main import MyModule as MyModule

Since you can now tell the SDK where the main object is located, with a Python entry point:

# pyproject.toml

[project.entry-points."dagger.mod"]
main_object = "my_module.main:MyModule"

If this setting doesn’t exist, the SDK will try to load from <package>:<MainObject>:

main_object = "my_module:MyModule"

And if that import fails, it will fallback to trying out main (e.g., for currently existing modules):

main_object = "main:MyModule"

This ensures backwards compatibility, and less limitations on code organization. The only one remaining is that it still needs to be a distribution package (installable).

Todo

@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch from 06f5146 to 44b209a Compare October 28, 2024 12:13
@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch 2 times, most recently from 7b95245 to 6a52c6e Compare November 3, 2024 18:44
@helderco helderco added this to the v0.14.0 milestone Nov 3, 2024
@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch from d700dfe to d614ea6 Compare November 4, 2024 16:51
@helderco
helderco marked this pull request as ready for review November 4, 2024 16:54
@helderco
helderco requested a review from a team as a code owner November 4, 2024 16:54
@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch 2 times, most recently from a2b3098 to 1f1e850 Compare November 6, 2024 16:27

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

One nit and looks like docs are pending; just want to make sure those are in by the release on Friday if we want this for v0.14.0 🙂

Comment on lines 1733 to +1781
func pythonSourceAt(modPath, contents string) dagger.WithContainerFunc {
return fileContents(path.Join(modPath, pythonSourcePath), contents)
return fileContents(path.Join(modPath, "src/test/__init__.py"), contents)

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.

nit: could the name of the module become a parameter rather than hardcoding test? Just seems like a gotcha to have to name the module test in order for this to work as expected (IIUC).

Realize this might be a super tedious update, not a blocker

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.

I think this is fine considering the alternative. If I make it a parameter then it becomes pretty tedious whenever this is used in any language. So I figured a convention for a module named "test" is simpler. There's test cases that use a different module name, they just don't use this helper.

@helderco

helderco commented Nov 6, 2024 •

Copy link
Copy Markdown
Contributor Author

One nit and looks like docs are pending; just want to make sure those are in by the release on Friday if we want this for v0.14.0 🙂

Yeah, those are coming tomorrow, I hope. I just need to prioritize dagger shell to get it in time too.

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great! Love that change

@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch from 1f1e850 to bb36bd7 Compare November 7, 2024 14:00
@helderco
helderco requested a review from a team as a code owner November 7, 2024 14:01
@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch from a9bac7a to bb36bd7 Compare November 7, 2024 14:02
@helderco

helderco commented Nov 7, 2024

Copy link
Copy Markdown
Contributor Author

Docs update in its own PR:

@helderco
helderco force-pushed the helder/dev-4565-python-allow-module-package-names-other-than-main branch from bb36bd7 to fb62b30 Compare November 7, 2024 17:53
@sipsma
sipsma merged commit 6b5c4bc into dagger:main Nov 7, 2024
@helderco
helderco deleted the helder/dev-4565-python-allow-module-package-names-other-than-main branch November 8, 2024 00:31
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.

Python: Allow module package names other than main

3 participants