-
Notifications
You must be signed in to change notification settings - Fork 124
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
Support defaults via kwargs #43
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -76,3 +76,32 @@ def hello(self, arg): | |
pm.register(Plugin()) | ||
res = pm.hook.hello(arg=3) | ||
assert res == 4 | ||
|
||
|
||
def test_defaults(pm): | ||
"""Verify that default keyword arguments can be declared on both specs | ||
and impls. The default value look up precedence is up as follows: | ||
- caller provided value | ||
- hookspec default | ||
- hookimpl default | ||
""" | ||
class Api: | ||
@hookspec | ||
def myhook(self, arg, kwarg="default"): | ||
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. Looking at this test I kind agree with the idea of only allowing 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. @nicoddemus yes this was the consensus prior as well. My main concern with this is we may end up with lots of |
||
"A spec with a default" | ||
|
||
class Plugin: | ||
@hookimpl | ||
def myhook(self, arg, kwarg="my default"): | ||
return kwarg | ||
|
||
pm.register(Plugin()) | ||
|
||
# with no spec registered | ||
assert pm.hook.myhook(arg='yeah!')[0] == "my default" | ||
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. 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. @nicoddemus no this is expected if you look at the code. If you want to raise an error due to missing |
||
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy" | ||
|
||
# with spec registered | ||
pm.add_hookspecs(Api) | ||
assert pm.hook.myhook(arg='yeah!')[0] == "default" | ||
assert pm.hook.myhook(arg='yeah!', kwarg='doggy')[0] == "doggy" |
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.
Even if we decide to abandon this defaults idea I really think we should keep this encapsulation of
hookspec
objects with a type much like we do withHookImpl
. It makes introspection tasks down the road that much simpler.