-
Notifications
You must be signed in to change notification settings - Fork 13
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
FEATURE: add partial
(args/kwargs) field to Action?
#52
Comments
Yes, we should add this. It will allow writing much cleaner code and avoid leaking memory. |
We discussed this recently and @Czaki was in favour of this and NOT @tlambert03 are you happy to take a PR to add |
I suspect the concert is strong refs inside the partial, is that true @Czaki? If so, it implies that the "proper" solution here would need to consider or reimplement all of the weak referencing stuff in psygnal. @lucyleeow i would definitely appreciate and consider a PR. Id say let's start with the simple/direct approach, not worrying about weakrefs, and see how it generally looks and feels |
Strong ref, harder debugging, less control. Promoting of bad patterns (not all callbacks are as good, prepared for
I think that we may start with a simple dict, but limit usage to basic types like And I'm not sure if such advanced things like in psygnal are required. As actions are global, how we expect to hardcode short living objects bind to Action. Or is there an obvious use case for short living actions? |
Nope I'm happy to consider these as long lived objects and just use a simple dict |
Revisiting things, could someone clarify, if we are limiting kwargs to basic types, how would this fix the strong refs inside partial problem? i.e., would passing an int via Or is the improvement here in regards to action kwargs being neater and the code thus easier to debug? Also I am not clear what you mean by partials providing "less control" @Czaki? Also if we were to implement action kwargs, would we use a partial e.g.,
or would you pass the kwargs when we execute the callback in Thanks cc @tlambert03 |
Because we do not worry about keeping reference to a string or float object. We worry about keeping reference to big object's live viewer or big numpy array.
I, personally, prefer kwargs as an action parameter, as it makes debugging easier. There will be no need to deal with inspecting partial. Also, I expect that people are looking to napari codebase for inspiration. And they may not notice subtle difference and use partial/lambdas as signal callback, that may lead to storing hard references to problematic objects and even segfaults.
I think that we should update in-n-out and pass kwargs directly. |
To clarify, if we were to expand kwargs to take complex objects (e.g., window/widgets) we would pass them safely: use a weak ref when passing to cc @Czaki |
We expect to not pass complex objects. We expect to pass |
Ah okay there is never an intention of passing more complex objects. |
... and if there is never an intention of passing more complex arguments, can you remind me again why
is so much better than
? It kinda seems like added code and complexity here for something that already has an acceptable solution |
Napari codebase is a place where people look for inspiration of how to write a code. Connecting It is also more readable when going through stacktrace when using debugger. |
not when all of your arguments are simple. We are far removed here from a simple connection to a signal. In fact, this isn't a direct connection to a signal. it's a connection to an in-n-out function, (whether you connect it to a signal or not is independent of app-model).
Leaving aside for a moment that this is not the napari repo, that argument is so vague and relatively self-important without much justification that it adds nearly nothing to the motivation for this feature on way or the other. A) it presumes that I'm looking for substantive discussion here, with concrete issues and proposals. Something more than a one or two line comment that just states as fact that your desired outcome is better.
is it? that kinda entirely depends on how the kwargs feature is added here and in in-n-out. This statement needs much justification |
You know this, I know this,and for example jni makes PR with lambdas connected to signals in examples (link.
No. This is to not inspire people to use Main difference between:
Is that in the first case the reference is stored in the
When we use kwargs, there is explicit injection of parameters to function, visible as steep in the debugger. When using partial, the injection happens on creation of Action time, and no one could easily use conditional breakpoint on injection steep without need to check value from partial. |
But that's only an issue because it's wrapping a complex object right? In a previous post you said "We expect to not pass complex objects. We expect to pass int, str, Enum, float etc." ... and so I asked "in that case, what's so bad about a partial", to which you responded by showing me code where Juan used a partial to bind a very complex object. Can you see why I'm confused with your position here? |
The general problem is that people without experience will not know that there is a difference between wrapping complex and non-complex object. But, as kwargs approach is not easy to apply to signals, it cannot be reproduced, by non-experienced user, as mistake that lead to problematic situation. |
perhaps one way we could go about this is for you to make a subclass if Action in It's not I don't think there's something potentially interesting here in this issue, but I'm not yet seeing a unambiguous and undeniable benefit or motivation behind it. |
I will be happy with such a solution. But As I check, it will also require changes in Command registry? As it will be easier than changing QAction related class? |
ok, see #194 which passes control of Action registration to the CommandRegistry. Note that the command registry has always been extendable: you need to pass your own custom subclass when you create your Application instance |
#194 is released in v0.2.7. So if you'd like to experiment more on the napari side, it should now be easier |
Should we add the ability store args/kwargs (like a partial) in an Action?
or should the callback just be the
partial
. Need to think about pros cons.first thought: the "pro" of just using
callback=partial(cb, a=1)
is that it's familiar regular python. The "con" is that it (currently) would require us to create a new command id for every partial variant. Maybe that's not a big dealThe text was updated successfully, but these errors were encountered: