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(sdk.v2): fixes broken output parameter type checking in _handle_single_return_value #6566

Merged
merged 3 commits into from
Sep 16, 2021

Conversation

judahrand
Copy link
Contributor

Description of your changes:
This function would fail under two scenarios:

  • typed list or dict output parameters (ie. Dict[str, str]) raised an incorrect ValueError
  • using list or dict rather than List or Dict raised an incorrect ValueError (PEP 585)

Checklist:

@google-oss-robot
Copy link

Hi @judahrand. Thanks for your PR.

I'm waiting for a kubeflow member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@judahrand judahrand changed the title fix(sdk.v2): fixes broken output parameter _is_parameter type checking fix(sdk.v2): fixes broken output parameter type checking in _handle_single_return_value Sep 14, 2021
@judahrand
Copy link
Contributor Author

judahrand commented Sep 14, 2021

This is one of two possible approaches.

  • This approach adds a dependency but checks the types inside a parameterized Generic (ie. list[int]). This has the advantage of failing before the JSON is serialized if unserializable objects are included in the list or dict.
  • The alternative is to steal what typeguard does to just check the origin types. This would look like this:
origin_type = getattr(annotation_type, '__origin__', annotation_type)
if type(return_value) != origin_type:
    raise ValueError(
        'Function `{}` returned value of type {}; want type {}'
        .format(self._func.__name__, type(return_value),
                origin_type))

This second option does not add a dependency but would also allow un-JSON-serializable types through regardless of the typehints. Given that I don't think we're currently checking whether the type hints point to something which is serializable perhaps the second approach is better?

Please let me know what you think.

@chensun
Copy link
Member

chensun commented Sep 15, 2021

This is one of two possible approaches.

  • This approach adds a dependency but checks the types inside a parameterized Generic (ie. list[int]). This has the advantage of failing before the JSON is serialized if unserializable objects are included in the list or dict.
  • The alternative is to steal what typeguard does to just check the origin types. This would look like this:
origin_type = getattr(annotation_type, '__origin__', annotation_type)
if type(return_value) != origin_type:
    raise ValueError(
        'Function `{}` returned value of type {}; want type {}'
        .format(self._func.__name__, type(return_value),
                origin_type))

This second option does not add a dependency but would also allow un-JSON-serializable types through regardless of the typehints. Given that I don't think we're currently checking whether the type hints point to something which is serializable perhaps the second approach is better?

Please let me know what you think.

@judahrand Thanks for the fix and the detailed comparison between the two approaches.
I tend to prefer the second option for no extra dependency.
The executor code is evaluated only at runtime, so I don't think there's much difference between failing before JSON serialization and failing during the serialization.

@chensun
Copy link
Member

chensun commented Sep 15, 2021

/ok-to-test

@judahrand
Copy link
Contributor Author

@chensun Sounds good, I've updated the PR to take that approach.

I believe there are quite a lot of other type checking issues throughout the SDK. A lot of them seem to revolve are Python 3.6 support.

@chensun
Copy link
Member

chensun commented Sep 16, 2021

/lgtm
/approve

Thanks!

@google-oss-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chensun

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot google-oss-robot merged commit 866dfc7 into kubeflow:master Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants