I am trying to deprecate property of class.
class A:
def __init__(self,
variable1: int,
##to be deprecated
variable2: int )
{....}
Expected behaviour: If user tries to use variable 2 he should get warning that its deprecated.
I am trying to deprecate property of class.
class A:
def __init__(self,
variable1: int,
##to be deprecated
variable2: int )
{....}
Expected behaviour: If user tries to use variable 2 he should get warning that its deprecated.
You can just give it a None
default and make sure it's not set:
import warnings
class A:
def __init__(
self,
variable1,
variable2=None,
):
if variable2 is not None:
warnings.warn(
"variable2 is deprecated", DeprecationWarning
)
Works with kwargs:
>>> A(1, variable2=123)
<ipython-input-4-e722737121fe>:12: DeprecationWarning: variable2 is deprecated
Works with positional args:
>>> A(1, 123)
<ipython-input-4-e722737121fe>:12: DeprecationWarning: variable2 is deprecated
You can implement variable2
as a property.
import warnings
class A:
def __init__(self, variable1: int, variable2: int):
self.variable1 = variable1
self._variable2 = variable2
@property
def variable2(self):
warnings.warn('The use of variable2 is deprecated.', DeprecationWarning)
return self._variable2
@variable2.setter
def variable2(self, value: int):
warnings.warn('The use of variable2 is deprecated.', DeprecationWarning)
self._variable2 = value