Passing method of non-WebSocketApp object as callback does not receive the WebSocketApp object as an argument
See original GitHub issuePrevious versions of websocket-client allowed passing methods of non-WebSocketApp objects as callbacks and have them receive the WebSocketApp object as the first argument. This was very convenient.
Recent versions (0.53, 0.54) introduced a change to _callback in _app.py that checks if the callback is a method. And if so, not pass the WebSocketApp object to the callback. This is fine if the callback method is bound to a WebSocketApp subclass, but very annoying if not.
Passing bound methods of other classes as callbacks is a very common pattern and it would be great if websocket-client could re-enable the original behavior for such cases.
A fix would be very simple, just one line of code:
def _callback(self, callback, *args):
if callback:
try:
if inspect.ismethod(callback) and isinstance(callback.__self__, type(self)):
callback(*args)
else:
callback(self, *args)
This change would allow 0.53/0.54 behavior for methods of WebSocketApp subclasses and 0.52 behavior for methods of objects that are not WebSocketApp subclasses.
As an aside, the older behavior seems more common in python modules with callbacks. I can understand the desire for the 0.53 change, but I think the more typical pattern is to have the subclass methods get called directly (i.e., have on_open, on_message, etc. methods in WebSocketApp that don’t do anything and are overridden in subclasses) if no callback attribute is specified. But that is, of course, up to the author.
Thanks
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
I got around this issue for now using a lambda function so, for example:
I hope this helps someone
@pbechliv just use 0.52.0. the changes starts from 0.53.0