1

I have a dispatcher that calls callbacks with two arguments:

class Foo:
    def register(self, callback):
        self.callback = callback

    def _handle(self):
        self.callback(x, y)

I wish to deprecate the second argument, and send only one argument instead:

def _handle(self):
    self.callback(x)

How am I able to deprecate the old callback without breaking code during the migration period?

Most ways to deprecate include ignoring an argument and sending DeprecationWarning. In this case it won't work as I have to send all parameters in case an older callback is registered.

Other ways are signature inspection (which won't be reliable) or creating a different register() function or new_callback parameter which looks like a code smell.

Bharel
  • 23,672
  • 5
  • 40
  • 80
  • 1
    This *may* be better suited for [softwareengineering.se] – juanpa.arrivillaga Feb 25 '20 at 11:13
  • Is `y` a value that could easily be supplied, or do you simply not have that value anymore after your refactoring? Would the callback code *typically* break if you passed `None` instead? Based on the answers to those two questions, I'd decide between keeping the code as is but issuing deprecation notices with a clear timeline, or starting to pass `None` and conditionally issue deprecation notices based on inspecting the callback signature. – deceze Feb 25 '20 at 11:36
  • I can pass a placeholder in `y`. In my specific case `y` is now `lambda: None`. Problem is, if someone will pass a callback that defines `*args`, inspection won't work, and I won't know if to call it with 1 or 2 parameters. If someone passes the new callback and I send 2, I'll give it too many parameters. If someone passes the old callback and I send 1, I'll give it too little. – Bharel Feb 25 '20 at 11:51
  • If `y` is a callable, then you could make *it* output a deprecation warning if called. – deceze Feb 25 '20 at 12:22
  • But I wish to deprecate it - i.e. not send it. – Bharel Mar 01 '20 at 18:03

0 Answers0