Skip to content

Python refactor: Load object from ID using new convention - #6184

Merged
helderco merged 1 commit into
dagger:mainfrom
helderco:sdk/python/codegen-refactor-idable-types
Dec 4, 2023
Merged

helderco merged 1 commit into
dagger:mainfrom
helderco:sdk/python/codegen-refactor-idable-types

Conversation

@helderco

@helderco helderco commented Nov 30, 2023 •

Copy link
Copy Markdown
Contributor

Follow-up to #5881

This is a refactor to simplify converting an ID scalar to object based on the new convention.

@helderco
helderco requested a review from vito November 30, 2023 15:51
@helderco

Copy link
Copy Markdown
Contributor Author

In draft only because of pending dependent merge. There's only one commit to review, which can be filtered in the files view.

Comment thread sdk/python/src/dagger/client/_guards.py 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.

(not blocker, just thinking out loud about general issues) Do we need to ban custom objects that end in ID? If someone did that in a module today and then wrote a python module that depended on it would it break anything here in particular (besides the breakage server-side, which may make this moot)

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.

No, it's not an issue here for two reasons. 🙂

First, this is matching only scalars that end in ID and modules today don't create scalars.

Secondly, this is only used in the HasID Protocol below and the runtime checking on protocols is pretty basic, it only checks for the existence of the attribute (in this case it should be checking if the object has an id method), it doesn't check the types.

So there isn't much value in IDScalar here, just static type checking and documentation or just putting it down for later use as the Is[] part isn't standard, it's part of the beartype library and I wanted to remember that it supports matching anything with a lambda like this.

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.

To be thorough, even if we allow a user to create custom scalars in the near future I don't think they will be able to create a method called id, which would have to return a custom scalar ending in ID. But even if they could, the worst that could happen would be:

  • On serialization that id method would be called before sending the result to the server;
  • On deserialization it would call loadXFromID to return the object, which is added by the server automatically on id-able objects;

Even then there's a chance that the local machinery from object_type will kick in for the above before trying to use id. Not sure, would have to test for that order but it's something that can be controlled if needed.

Follow-up to dagger#5881, this is a refactor to simplify converting an ID scalar to object based on the new convention.

Signed-off-by: Helder Correia <[email protected]>
@helderco
helderco force-pushed the sdk/python/codegen-refactor-idable-types branch from 70654ec to 0ecf003 Compare December 4, 2023 12:54
@helderco
helderco marked this pull request as ready for review December 4, 2023 12:54
@helderco helderco changed the title Python: Load object from ID using new convention Python refactor: Load object from ID using new convention Dec 4, 2023
@helderco
helderco merged commit 3bdef92 into dagger:main Dec 4, 2023
@helderco
helderco deleted the sdk/python/codegen-refactor-idable-types branch December 4, 2023 13:25
@helderco helderco mentioned this pull request Dec 6, 2023
3 of 4 tasks
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