Skip to content

@magic_factory doesn't work as a method descriptor properly #733

Description

@Antyos

Describe the bug
Using @magic_factory on a class method will not properly pass self to its generated function guis, likely because it is using functools.partial instead of functools.partialmethod internally.

To Reproduce

from magicgui import magic_factory


class C:
    def __init__(self, x: int):
        self.x = x
        self.func = self.func_fact()

    @magic_factory()
    def func_fact(self):
        print(self.x)

    # In in ideal world, this would also work:
    # func = func_fact()


c = C(x=5)

c.func()
# c.func_fact()()  # This also fails
Traceback

c:\Users\andrew\Code\project\code\sandbox\magic_factory_method.py:7: FutureWarning: functools.partial will be a method descriptor in future Python versions; wrap it in staticmethod() if you want to preserve the old behavior
  self.func = self.func_fact()
Traceback (most recent call last):
  File "c:\Users\andrew\Code\project\code\sandbox\magic_factory_method.py", line 16, in <module>
    c.func()
    ~~~~~~^^
  File "C:\Users\andrew\Code\project\code\.venv\Lib\site-packages\magicgui\widgets\_function_gui.py", line 338, in __call__
    raise TypeError(msg) from None
TypeError: missing a required argument: 'self' in call to 'func_fact(self)'.
To avoid this error, you can bind a value or callback to the parameter:

    func_fact.self.bind(value)

Or use the 'bind' option in the magicgui decorator:

    @magicgui(self={'bind': value})
    def func_fact(self): ...

Expected behavior
I would expect @magic_factory to work on a class method.

Environment (please complete the following information):

  • OS: Windows, Python 3.13.9
  • backend: PyQt6==6.9.1
  • magicgui version: from main branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions