Deprecation warnings with Python 3.8

As you’ve all probably seen, the Python 3.8 DeprecationWarning has been elevated for a number of various bits and I’m seeing a lot of them that may indicate missing pieces in the wrappers.

Here’s one:

    item = wx.MenuItem(menu, id, text, kind=wx.ITEM_NORMAL)

If id is an int or wx._core.StandardID everyone is happy, but if it’s a wx._core.WindowIDRef, then I see

DeprecationWarning: an integer is required (got type WindowIDRef).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.

Another example here:

wx\core.py:1477: DeprecationWarning: an integer is required (got type WindowIDRef).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.

Where the (trimmed down) code from core.py looks like this, line 1477 is the call to Connect:

class PyEventBinder(object):
    def Bind(self, target, id1, id2, function):
        for et in self.evtType:
            target.Connect(id1, id2, et, function)

Is there a way to define the WindowIDRef class such that it casts itself to an int like StandardID apparently does so as to fix these warnings?

After researching this a bit, it looks pretty simple: just add an __index__ method on WindowIDRef.

The following hack eliminates this warning.

import wx

wx._core.WindowIDRef.__index__ = wx._core.WindowIDRef.__int__

app = wx.App()
menu = wx.Menu()
i = wx._core.WindowIDRef()
wx.MenuItem(menu, i, 'a', kind=wx.ITEM_NORMAL)