-
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
Iterate plugin results #50
Comments
at first glance this will be a massive pain and incompatible with hookwrappers i believe we should have n actually discussion about the patterns of control that happen, perhaps take a look at stevedore as well otherwise we will just repeat the stockpiling of minimal changes that made the pytest codebase a painful mess |
@fschulze if I understand this correctly you're looking for something like: for result in pm.hook.myproject_get_password(arg1=1, arg2=2):
if not result: # skip to the next hookimpl
continue
else:
success = unlock_with_pw(result)
if success:
break Where each iteration lazily invokes each underlying @RonnyPfannschmidt actually this wouldn't be incompatible with wrappers as long as we can remove the recursion non-sense from |
@fschulze I looked into this briefly and it seems to me in order to avoid transforming We could try it as a trial feature that we don't have to commit to keeping but I'd want to hear what everyone else thinks. |
@goodboy do you have that experiment ready? if yes i propose to just do a RFC pr and others taking a look, we should probably let that one trigger a futurewarning thats not hidden |
@RonnyPfannschmidt it's basically just rewriting that function to I can draft up something if I get the time this weekend. |
I agree, if it is something simple we can discuss it over a PR.
IIUC, actually we would change the existing interface, no? Currently I'm 👍 on the idea, Python itself change its methods which returned lists to iterator-like ( It will break the API, but now big deal because it should be simple to fix and we can coordinate that with pytest, tox and devpi. |
well, actually - its not really a api break to intorduce it as aoption, but it would make hookwrapper incompatible for example as such we need to take a close look on how to express this api because its effects are far from simple |
Hmm can you explain why? After all even if the hook caller decides to stop the iteration, the "after" hooks can still be called I believe. |
Yep, totally agree it's straightforward. iter_hooks = pm.ihook.myproject_get_password(arg1=1, arg2=2)
for result in iter_hooks:
if not result: # skip to the next hookimpl
continue
else:
success = unlock_with_pw(result)
if success:
# don't call any more hooks and teardown
iter_hooks.send("None") Scratch that you can do it with a |
my gut feeling screams this will kill us with strange edge cases unless we correctly combine it with context management in some ways |
@RonnyPfannschmidt I could see that yeah. I like context managers 👍 - explicit is always better. |
Just breaking out of the loop should work, so I think this should be possible: iter_hooks = pm.ihook.myproject_get_password(arg1=1, arg2=2)
for result in iter_hooks:
if not result: # skip to the next hookimpl
continue
else:
success = unlock_with_pw(result)
if success:
break Or more succinctly: iter_hooks = pm.ihook.myproject_get_password(arg1=1, arg2=2)
for result in iter_hooks:
success = unlock_with_pw(result)
if success:
break And hook wrappers would still work, you can call pre/post if you use a try/finally while yielding the hook results. Here's a short demonstration: def call_hooks():
r = []
try:
hool_impls = range(5)
for x in hool_impls:
yield x
r.append(x)
finally:
print('call post hookwrappers', r)
for i in call_hooks():
print('hook', i)
if i == 2:
print('breaking')
break This prints:
|
(I just noticed @tgoodlet pushed a PR, I will review it in the context of this implementation when I get the chance) |
@fschulze @goodboy @nicoddemus i have just been thinking, why not inverse the flow of control in the example iter_hooks = pm.ihook.myproject_get_password(arg1=1, arg2=2)
for result in iter_hooks:
if not result: # skip to the next hookimpl
continue
else:
success = unlock_with_pw(result)
if success:
break is the wrong way around class KeyringPlugin:
def __init__(self, keyring):
...
def myproject_unlock_identity(unlock_with_pw ,arg1, arg2)
try:
password = ...
except (ValueError, LookupError):
pass
else:
return unlock_with_pw(password)
# firstresult hook
success = pm.ihook.myproject_unlock_identity(unlock_with_pw=unlock_with_pw,arg1=1, arg2=2) unless someone provides a example where that pattern cannot hope to work, i propose we drop the idea and keep pluggy simpler |
Good point. I'm OK with suggesting that alternative. If we agree @RonnyPfannschmidt suggestion is reasonable, this would then not be a 1.0 release blocker, I assume. |
That way the KeyringPlugin needs to know about the password plugin. I don't think this should block a 1.0 either way, but I still think if we find a good way to implement it, the feature would be worth adding. Do we have a low priority label we could set on this ticket? |
Currently we have the
firstresult
option. An interesting addition would be a way to iterate over the plugins. The big difference to the default of returning all results, is that the consumer can stop iterating at any point. This is useful for things where plugins might cause user interaction. One example is a password hook that tries to get a password from a keychain and if that doesn't work the next plugin is tried, which asks the user for the password in a prompt. Withfirstresult
we don't get the fallback and with the current default of returning all results, the user would always be prompted, even if the password from the keychain would work.The text was updated successfully, but these errors were encountered: