-
Notifications
You must be signed in to change notification settings - Fork 427
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
Changes from 2 commits
fb159ce
2865b77
db93801
98f68b7
ef3af42
e8dd3ab
57a2a80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,7 +303,12 @@ class Accessor(str): | |
"Failed lookup for key [{key}] in {context}, when resolving the accessor {accessor}" | ||
) | ||
|
||
def __new__(cls, value): | ||
def __init__(self, object, *callable_args, **callable_kwargs): | ||
self.callable_args = callable_args | ||
self.callable_kwargs = callable_kwargs | ||
super().__init__() | ||
|
||
def __new__(cls, value, *args, **kwargs): | ||
instance = super().__new__(cls, value) | ||
if cls.LEGACY_SEPARATOR in value: | ||
instance.SEPARATOR = cls.LEGACY_SEPARATOR | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure on the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
# Important that we break in None case, or a relationship | ||
# spanning across a null-key will raise an exception in the | ||
# next iteration, instead of defaulting. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,17 @@ def test_short_circuit_dict(self): | |
|
||
self.assertEqual(Accessor("occupation__name").resolve(context), "Carpenter") | ||
|
||
def test_callable_args_kwargs(self): | ||
class MyClass: | ||
def method(self, *args, **kwargs): | ||
return args, kwargs | ||
|
||
callable_args = ("arg1", "arg2") | ||
callable_kwargs = {"kwarg1": "val1", "kwarg2": "val2"} | ||
obj = MyClass() | ||
result = Accessor("method", *callable_args, **callable_kwargs).resolve(obj) | ||
self.assertEqual(result, (callable_args, callable_kwargs)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next step: supporting |
||
|
||
|
||
class AccessorTestModel(models.Model): | ||
foo = models.CharField(max_length=20) | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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