0

I would like to know how to solve this problem:

This throws error (GUI is not known at the point of processing gui : GUI):

class AppLogic():
    
    gui : GUI

    def __init__(self) -> None:
        self.gui = None

    def image_clicked(self):
        pass #not important
    
class GUI():

    app_logic : AppLogic
    
    def __init__(self) -> None:
        self.app_logic = None
        
    def render_image(self):
        pass #not important

app = AppLogic()
gui = GUI()

app.gui = gui
gui.app_logic = app

I can delete the explicit data type declaration, but at that moment, I will lose all the intellisense power when I would like to use hints while using the attribute gui or app_logic inside the class definition. Does it have any solution to make intellisense working in this situation?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Does this answer your question? [How do I type hint a method with the type of the enclosing class?](https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class) – mkrieger1 Nov 15 '22 at 19:49
  • 1
    `GUI` is the wrong type if `self.gui = None` should be allowed. – chepner Nov 15 '22 at 19:57
  • 1
    It should probably be `gui: Optional['GUI']`, so that you can create the `AppLogic`, define `GUI` with the existing `AppLogic` instance as an argument so that you can initialize the `app_logic` attribute *immediately*, then assign `app.gui = gui` afterwards. (It's not possible to define the two objects with references to each other simultaneously.) – chepner Nov 15 '22 at 20:03
  • (It seems to make more sense to have an application with no GUI than a GUI that's not associated with an application.) – chepner Nov 15 '22 at 20:03
  • @mkrieger1 yes, thats it. Forward references. Saved my day. :) Thanks – Daniel Šebík Nov 15 '22 at 20:18
  • @chepner I don't know what do you mean by not having GUI associated with application. I am learning PyQt on my own, and I have never seen any common GUI app achitectures (I am C embedded SW engineer :) ). But I am interested in this architecture topic, but don't know where to start. Do you have some tips? :) – Daniel Šebík Nov 15 '22 at 20:25
  • I mean, you could have a command-line application that has no GUI, but it's much less useful to have a GUI that isn't an interface to any particular application. – chepner Nov 15 '22 at 20:29
  • And why do you think think the GUI will work badly? I just wanted to seperate rendering of different GUI blocks from the app logic. Is that bad idea? – Daniel Šebík Nov 15 '22 at 20:53

0 Answers0