Hi everyone,
I’m relatively new to wxPython and encountered what I believe might be a type hint inconsistency in the Python interface (pyi) files.
In the pyi files, wx.GetApp() is annotated as returning an AppConsole type:
def GetApp() -> AppConsole:
"""
GetApp() -> AppConsole
Returns the current application object.
"""
However, when I try to use wx.GetApp().GetTopWindow(), my IDE (using the type hints) warns me that AppConsole has no method GetTopWindow. This seems misleading because in practice, wx.GetApp() does return an object that has the GetTopWindow method.
I tested this with a simple example:
import wx
app = wx.App()
print(app) # Output: <wx.core.App object at 0x...>
print(wx.GetApp()) # Output: <wx.core.App object at 0x...>
Both app and wx.GetApp() point to the same object, which is an instance of wx.App (not AppConsole), and wx.App does have the GetTopWindow method.
This suggests that the return type hint for wx.GetApp() should probably be App rather than AppConsole.
My Question:
I’m currently working around this by manually modifying the type hint to return App instead, which resolves the IDE warnings and works correctly in practice. I’m posting this question to confirm whether this is indeed a labeling error in the type hints, or if there are other considerations behind using AppConsole . If this is indeed an error that can be fixed, that would be great for future users!
Thank you for reading this far.
You don’t mention what is your IDE, but I assume is one of the most frequently mentioned: VSCode or PyCharm.
About your question, I “think” it depends of what the IDE uses to run the functions names discovery.
If on Windows results may differ if using pythonw versus python. I am not familiar with python interfaces files, but usually interfaces are oriented for console apps. Does it make a difference if you are using .py files?
(of course more advanced users may respond)
1 Like
I believe wx.GetApp() should return wx.App and not wx.AppConsole.
Looking at the inheritance diagram of wx.App in the wxPython documentation:
https://docs.wxpython.org/wx.App.html
It’s obvious that wx.App derives from wx.AppConsole. However, the object returned by wx.GetApp() is not wx.AppConsole so I’d suggest submitting a PR - I’m sure @swt2c will review it.
1 Like
Well, if you instantiate a wx.AppConsole then wx.GetApp() should return it.
Sure, creating a wx appliacation without a GUI is not a common use case - if it’s used at all.
Anyway, I don’t think that it’s worth changing things and it would take years until it would propagate into wxPython.
If you’re doing major things with wx.GetApp(), then you’re probably doing something wrong. If you really need it, do app = wx.GetApp(), set a breakpoint after it and develop with the debugger. Having live objects will help auto-completion a lot…
2 Likes
The issue is on pyi stub files, hardly a problem that would take years to propagate to wxPython. To me it doesn’t look like it’s automatically generated from wxWidgets so it’s just a one-line patch for wxPython.
While I agree it may return wx.AppConsole if someone is not using wxPython as a GUI framework, I find this possibility exceedingly unlikely.
At the same time, I dislike type hints and annotations so much that I do not care whether this gets changed or not.
2 Likes
Yes, I’m not a big fan either.
Interactive development in a shell is so much more fun (either in the wxPython application itselt or in the IDE debugger).
I learned that with FORTH decades ago…
2 Likes
Thanks, everyone. I’ll just modify the .pyi file locally.
Thanks also to Andrea_Gavana for the reference and suggestion.