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

[Local] Support passing more parameters to handler if handler contains **kwargs keyword #3533

Conversation

Tankilevitch
Copy link
Contributor

@Tankilevitch Tankilevitch commented May 11, 2023

@Tankilevitch Tankilevitch marked this pull request as ready for review May 14, 2023 08:40
Copy link
Member

@alonmr alonmr left a comment

Choose a reason for hiding this comment

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

Looks good, does this also work with *args?
Also, I get_func_arg is also used with nuclio runtime, We should make sure it works there as well.

tests/run/assets/kwargs.py Show resolved Hide resolved
Copy link
Member

@theSaarco theSaarco left a comment

Choose a reason for hiding this comment

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

There's some logic missing, I believe.

kwargs[key] = copy(params[key])
for key in inputs:
if key not in kwargs:
kwargs[key] = context.get_input(key, inputs[key])
Copy link
Member

Choose a reason for hiding this comment

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

The input processing above has more to it (using obj.local() in some cases) - maybe have that piece of logic in an internal function, and use it here as well. Or did you verify this is dead code in which case I would remove it there as well?

Copy link
Member

@guy1992l guy1992l left a comment

Choose a reason for hiding this comment

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

Straight to the point 💪
Just a minor suggestion.


# VAR_KEYWORD meaning : A dict of keyword arguments that aren’t bound to any other parameter.
# This corresponds to a **kwargs parameter in a Python function definition.
if any(param.kind == param.VAR_KEYWORD for param in args.values()):
Copy link
Member

Choose a reason for hiding this comment

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

args = inspect.signature(handler).parameters means that args is an OrderedDict and because **kwargs can only be last in the function's parameters list, you can simply check the last one:

Suggested change
if any(param.kind == param.VAR_KEYWORD for param in args.values()):
if list(args.values())[-1].kind == param.VAR_KEYWORD:

Copy link
Member

@alonmr alonmr left a comment

Choose a reason for hiding this comment

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

minor

Comment on lines 480 to 481
else:
return context.get_input(input_key, inputs[input_key])
Copy link
Member

Choose a reason for hiding this comment

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

I believe it's the same

Suggested change
else:
return context.get_input(input_key, inputs[input_key])
return input_obj

@@ -142,6 +142,32 @@ def test_local_runtime_failure_before_executing_the_function_code(db):
assert "failed on pre-loading" in str(exc.value)


def test_local_runtime_with_kwargs(db):
Copy link
Member

Choose a reason for hiding this comment

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

would be nice if you could add some more test cases for example:

  1. if kwargs is empty
  2. if params is empty
  3. if param x is in kwargs

Copy link
Member

@theSaarco theSaarco left a comment

Choose a reason for hiding this comment

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

Looks good.

Copy link
Member

@liranbg liranbg left a comment

Choose a reason for hiding this comment

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

Minor

kwargs[key] = _get_input_value(key)

# get the last parameter, as **kwargs can only be last in the function's parameters list
last_param = list(args.values())[-1]
Copy link
Member

Choose a reason for hiding this comment

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

What if list is empty ?

@alonmr alonmr merged commit 00d1719 into mlrun:development May 15, 2023
14 checks passed
Tankilevitch added a commit to Tankilevitch/mlrun that referenced this pull request May 22, 2023
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.

None yet

5 participants