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

Add support to pass args/kwargs to callables from Accessor #940

Merged
merged 7 commits into from
Apr 11, 2024

Conversation

JordanHyatt
Copy link
Contributor

@JordanHyatt JordanHyatt commented Dec 8, 2023

Fixes: #939

Implemented the feature request and wrote the required unit text

@jieter jieter changed the title Issue939 Allow args/kwargs to be passed to a callable with an Accessor Add support to pass args/kwargs to callables from Accessor Feb 6, 2024
self.callable_kwargs = callable_kwargs
super().__init__()

def __new__(cls, value, *args, **kwargs):
Copy link
Owner

Choose a reason for hiding this comment

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

__new__ did not have *args, **kwargs before, does the fix need that?

Copy link
Contributor Author

@JordanHyatt JordanHyatt Feb 10, 2024

Choose a reason for hiding this comment

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

__new__ and __init__ need to have compatible signatures, I updated it so that it is explicit now

@@ -394,7 +399,7 @@ def resolve(self, context, safe=True, quiet=False):
if safe and getattr(current, "alters_data", False):
raise ValueError(self.ALTERS_DATA_ERROR_FMT.format(method=current.__name__))
if not getattr(current, "do_not_call_in_templates", False):
current = current()
current = current(*self.callable_args, **self.callable_kwargs)
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure on the callable_-prefix, can we simplify this by just calling them self.args and self.kwargs?

Copy link
Contributor Author

@JordanHyatt JordanHyatt Feb 10, 2024

Choose a reason for hiding this comment

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

I kind of like it being more verbose as it tells the user what those args and kwargs are being used for. If you disagree, I can make them be args and kwargs no problem. Another option is to not wild-card them and have the user pass them as a list/dict instead, this better indicates that they are "pass-through" args/kwargs. I am going to change it to the later and you let me know what you think

callable_kwargs = {"kwarg1": "val1", "kwarg2": "val2"}
obj = MyClass()
result = Accessor("method", *callable_args, **callable_kwargs).resolve(obj)
self.assertEqual(result, (callable_args, callable_kwargs))
Copy link
Owner

Choose a reason for hiding this comment

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

Next step: supporting Accessor as arg/kwarg to support even more dynamic constructs 😬

@jieter
Copy link
Owner

jieter commented Feb 6, 2024

Thanks for your contribution @JordanHyatt. Please have a look at my questions.

Copy link
Owner

@jieter jieter 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, thanks!

@jieter jieter merged commit 4c81fd2 into jieter:master Apr 11, 2024
15 checks passed
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.

Feature Request: Allow args/kwargs to be passed to a callable with an Accessor
2 participants