I am a relatively new maintainer of a burgeoning Python library that is used by several dozen users. We have a function with a positional argument that needs to be deprecated. For example, our function is something like:
def foo(x, y, z, a, b = None, c = None, d = 1):
...
and we are looking to depreciate and ultimately remove y
, while not immediately breaking code compatibility. What is the best/most pythonic way to do this? The best idea that I have at the moment is to move z
and a
to keyword arguments and change the definition to:
def foo(*args, **kwargs):
if len(args)>=2 or 'a' not in kwargs:
warnings.warn("y is deprecated, pass in z and a as keyword arguments")
However, this obviously makes the call more annoying for documentation and accounting purposes (e.g., what are the possible kwargs
). Is there a better way to depreciate a positional argument?
I should add that it would be best to not break any current code for a bit and to warn users if we are going to break their code.