8000 feat: separate out enum names and values by jedevc · Pull Request #9518 · dagger/dagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: separate out enum names and values #9518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 23, 2025
Merged

feat: separate out enum names and values #9518

merged 14 commits into from
Jun 23, 2025

Conversation

jedevc
Copy link
Member
@jedevc jedevc commented Feb 6, 2025

Fixes #8671 - the aim is to fix all the remaining little nits with enums that make them tricky to use (also wanting to unblock #8608).

Warning

👷‍♀️ Depends on #10566.

TODO:

  • Tidy up core enum names
  • Improved withEnumMember API for enum members
  • Kick off migration from withEnumValue to withEnumMember in all SDKs
    • Go
    • Python
    • Typescript (skipping for now, turns out this is just really hard to do, since the typescript enums aren't richly typed).
  • Backwards compat (this is gonna be bit tricky)
    • Handle renamed types, ImageMediaType->ImageMediaTypes, EnumValueTypeDef->EnumMemberTypeDef (deferred into Module version compat should handle type changes #10103)
    • Ensure that codegen for enums in dependencies stays the same
    • Keep old TypeDef field EnumTypeDef.values
    • Keep old core enum values (using member aliases)
    • Add new legacy tests
  • Fix inevitably broken tests
  • Clean up

@jedevc jedevc added the area/engine About dagger core engine label Feb 6, 2025
@jedevc jedevc force-pushed the fix-enums branch 4 times, most recently from 5cf1dde to 79740f7 Compare February 7, 2025 17:31
@jedevc jedevc force-pushed the fix-enums branch 8 times, most recently from 56d2a80 to f71c45a Compare February 20, 2025 14:45
Copy link
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Copy link
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Copy link
Contributor
github-actions bot commented Apr 5, 2025

This PR was closed because it has been stalled for 7 days with no activity.

@jedevc
Copy link
Member Author
jedevc commented Apr 9, 2025

I split some work out to #10058, so will rebase on that once merged.

Copy link
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Copy link
Contributor
github-actions bot commented May 1, 2025

This PR was closed because it has been stalled for 7 days with no activity.

Copy link
Contributor
@helderco helderco left a comment

Choose a reason for hiding this comment

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

This took effort, thanks for sticking with it! 😄 Looking great!

Comment on lines 62 to 69
conv.register_unstructure_hook(enum.Enum, to_enum_name)
conv.register_structure_hook(enum.Enum, from_enum_name)

conv.register_unstructure_hook(base.Enum, to_enum_name)
conv.register_structure_hook(base.Enum, from_enum_name)

conv.register_unstructure_hook(Enum, to_enum_name)
conv.register_structure_hook(Enum, from_enum_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid duplication here by using your own predicate function:

Suggested change
conv.register_unstructure_hook(enum.Enum, to_enum_name)
conv.register_structure_hook(enum.Enum, from_enum_name)
conv.register_unstructure_hook(base.Enum, to_enum_name)
conv.register_structure_hook(base.Enum, from_enum_name)
conv.register_unstructure_hook(Enum, to_enum_name)
conv.register_structure_hook(Enum, from_enum_name)
def is_enum(t) -> bool:
return issubclass(t, enum.Enum)
conv.register_unstructure_hook_func(is_enum, to_enum_name)
conv.register_structure_hook_func(is_enum, from_enum_name)

This comment was marked as resolved.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm. So this doesn't work. a2fd403

https://dagger.cloud/jedevc/traces/26e7a81edf9304da7090245ef15893fe

I could be missing something obvious.

But I seem to remember having this conversation in discord here: https://discord.com/channels/707636530424053791/1337048414751559680/1341125495189798962

Copy link
Contributor

Choose a reason for hiding this comment

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

I refactored a bit to allow for it, pushed to your branch.

Basically the dagger.client.base.Enum class extended str making members of the class basically strings. The problem is that cattrs would dispatch the hook for "str" when encountering a member, instead of the custom added. We could override the hook for "str" too but I want to simplify.

I want to deprecate dagger.Enum in favor of enum.Enum:

Still added a type hook (instead of function hook) if an enum decorated with @dagger.enum_type extends a primitive type like str or int for backwards compat or if users end up using it on their own directly.

So this should cover all enums in a simple way without having to register 3 different types and still missing cases (e.g., user extending str and enum.Enum without one of our base classes).

jedevc added 12 commits June 20, 2025 11:45
Signed-off-by: Justin Chadwell <me@jedevc.com>
This allows enums:
- to have associated values, as well as their pre-existing names
- to have versioned members using dagql views

Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
This can happen after weird string processing.

Signed-off-by: Justin Chadwell <me@jedevc.com>
Enums are complicated. So handle them separately, instead of happily
bundling them into primitive types.

Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: Justin Chadwell <me@jedevc.com>
helderco added 2 commits June 21, 2025 16:33
I want to deprecate extending dagger.Enum in favor of stdlib's enum.Enum in a follow-up PR so removed str as a base class from dagger.client.base.Enum and adjusted tests to more explicitly return the value instead of the string representation. Additionallly motivated because in cattrs it bypasses the custom hook for enums, and using the one for primitive strings instead, making registration a bit more complicated.

Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
Copy link
Contributor
@helderco helderco left a comment

Choose a reason for hiding this comment

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

🎉

@jedevc jedevc merged commit aaa13be into dagger:main Jun 23, 2025
57 checks passed
@jedevc jedevc added this to the v0.18.11 milestone Jun 23, 2025
TomChv added a commit to TomChv/dagger that referenced this pull request Jun 23, 2025
Update TS SDK to support enum/value feature introduced by dagger#9518.
Handle serialization through metadata with an external function generated in client.
Handle deserialization inside client gen with an function generated in client.
Add tests for enum generation

Signed-off-by: Tom Chauveau <tom@dagger.io>
TomChv added a commit to TomChv/dagger that referenced this pull request Jun 24, 2025
Update TS SDK to support enum/value feature introduced by dagger#9518.
Handle serialization through metadata with an external function generated in client.
Handle deserialization inside client gen with an function generated in client.
Add tests for enum generation

Signed-off-by: Tom Chauveau <tom@dagger.io>
TomChv added a commit to TomChv/dagger that referenced this pull request Jun 24, 2025
Update TS SDK to support enum/value feature introduced by dagger#9518.
Handle serialization through metadata with an external function generated in client.
Handle deserialization inside client gen with an function generated in client.
Add tests for enum generation

Signed-off-by: Tom Chauveau <tom@dagger.io>
TomChv added a commit to TomChv/dagger that referenced this pull request Jun 25, 2025
Update TS SDK to support enum/value feature introduced by dagger#9518.
Handle serialization through metadata with an external function generated in client.
Handle deserialization inside client gen with an function generated in client.
Add tests for enum generation

Signed-off-by: Tom Chauveau <tom@dagger.io>
TomChv added a commit to TomChv/dagger that referenced this pull request Jun 26, 2025
Update TS SDK to support enum/value feature introduced by dagger#9518.
Handle serialization through metadata with an external function generated in client.
Handle deserialization inside client gen with an function generated in client.
Add tests for enum generation

Signed-off-by: Tom Chauveau <tom@dagger.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/engine About dagger core engine
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improved custom enum support
3 participants
0