Skip to content
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

fix: add default value for optional fields #11

Merged
merged 1 commit into from
Nov 11, 2024
Merged

fix: add default value for optional fields #11

merged 1 commit into from
Nov 11, 2024

Conversation

zshipko
Copy link
Contributor

@zshipko zshipko commented Nov 8, 2024

Closes #10

Currently when constructing a type with nullable fields the user is required to explicitly pass None if no argument is needed.

This sets nullable fields to None by default - to do this we need to list all non-nullable fields first, then the nullable ones because Python requires arguments with default values to be listed last. This means the order of arguments for type constructors might be in a different order from the schema and other PDKs, but I'm not sure there is another way around this.

@zshipko zshipko requested a review from bhelx as a code owner November 8, 2024 23:42
<%- p.name %>: <%- toPythonType(p) %>
<% } %>
Copy link
Contributor

Choose a reason for hiding this comment

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

If the only difference b/w these two loops is the nullable part. maybe we can merge them something like this?


@dataclass
class <%- capitalize(schema.name) %>(extism.Json):
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
  # <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<% if (p.nullable) {%>
  <%- p.name %>: <%- toPythonType(p) %> = None
<% } else { %>
  <%- p.name %>: <%- toPythonType(p) %>
<% } %>
<% }) %>

<% } %>
<% }); %>

or just customize the = None part

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an ordering issue, the fields with default values have to come after the fields with no defaults.

@zshipko zshipko merged commit 4828a02 into main Nov 11, 2024
1 check passed
@zshipko zshipko deleted the default-none branch November 11, 2024 18:44
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.

Improve handling of nullable fields
2 participants