Skip to content

Better type checking integration for my_gui_class.gui #691

@multimeric

Description

@multimeric

Consider this example:

from magicgui.experimental import guiclass

@guiclass
class MyDataclass:
    number: int = 0

if __name__ == '__main__':
    obj = MyDataclass()
    obj.gui.show(run=True)

Running pyright widget.py gives:

widget.py:10:9 - error: Cannot access member "gui" for type "MyDataclass"
    Member "gui" is unknown (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 

Obviously the type checker can't know about the gui field that gets added to the class.

If you are interested in suggestions, dataclass_transform can be used on a class not just a function, which allows you to create a superclass that has a gui property or field but also acts like a dataclass:

from magicgui.experimental import GuiClass

class MyDataclass(GuiClass):
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions