magicgui icon indicating copy to clipboard operation
magicgui copied to clipboard

The `persist` argument is ignored in `@magicgui(persist=True)` decorator when used inside a class

Open ximeg opened this issue 2 years ago • 1 comments

Describe the bug I noticed that when I use @magicgui decorator on a class method, it becomes somewhat buggy and ignores some of its arguments. Noticeably, the argument persist does not have an effect: the cache file is not created, and the latest value is not saved.

However, if I create GUI by calling magicgui() directly on a class method, everything works just fine.

To Reproduce Below are two minimal working examples to reproduce this behavior. The first one uses the decorator, while the second one does not. Both snippets should have the same behavior, but the first one does not create a cache file.

# MWE WITH A DECORATOR - CACHE ISN'T WORKING
import napari
from magicgui import magicgui


class MyClass(object):
    @magicgui(persist=True)
    def example(self, a=0):
        ...


m = MyClass()

v = napari.Viewer()
v.window.add_dock_widget(m.example)

napari.run()

# MWE WITHOUT A DECORATOR - CACHE WORKS
import napari
from magicgui import magicgui


class MyClass(object):
    def __init__(self):
        self.example = magicgui(self._example, persist=True)

    def _example(self, a=0):
        ...


m = MyClass()

v = napari.Viewer()
v.window.add_dock_widget(m.example)

napari.run()

Expected behavior I expect the same behavior whether I use a decorator inside or outside of a class, i.e. caching of the widget values if persist=True argument was provided to a decorator.

Environment (please complete the following information):

  • magicgui version 0.3.7
  • napari: 0.4.14
  • Platform: Windows-10-10.0.19041-SP0
  • Python: 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]
  • Qt: 5.15.2
  • PyQt5: 5.15.6

ximeg avatar Mar 30 '22 18:03 ximeg

P.S. If I use the @magicgui decorator outside the class, I don't experience this issue.

ximeg avatar Mar 30 '22 18:03 ximeg

Thanks @ximeg and sorry for the delay, this will be fixed in https://github.com/napari/magicgui/pull/469

tlambert03 avatar Oct 22 '22 14:10 tlambert03