3
3
4
4
def deprecated (reason ):
5
5
def decorator (cls ):
6
- original_init = cls .__init__ # Save the original __init__
6
+ if cls .__init__ is object .__init__ :
7
+ # If cls.__init__ is object.__init__, define a basic __init__ to wrap
8
+ def new_init (self , * args , ** kwargs ):
9
+ warnings .warn (f"{ cls .__name__ } is deprecated: { reason } " ,
10
+ category = DeprecationWarning , stacklevel = 2 )
11
+ else :
12
+ original_init = cls .__init__ # Save the original __init__
7
13
8
- @functools .wraps (cls .__init__ )
9
- def new_init (self , * args , ** kwargs ):
10
- # Emit the deprecation warning whenever the class is instantiated
11
- warnings .warn (f"{ cls .__name__ } is deprecated: { reason } " ,
12
- category = DeprecationWarning , stacklevel = 2 )
13
- original_init (self , * args , ** kwargs ) # Call the original __init__
14
+ @functools .wraps (cls .__init__ )
15
+ def new_init (self , * args , ** kwargs ):
16
+ warnings .warn (f"{ cls .__name__ } is deprecated: { reason } " ,
17
+ category = DeprecationWarning , stacklevel = 2 )
18
+ original_init (self , * args , ** kwargs ) # Call the original __init__
14
19
15
20
cls .__init__ = new_init
16
21
return cls
22
+
17
23
return decorator
0 commit comments