I have a class in a different module and I want to show a DeprecationWarning
when that class is imported. What will be the right way to do so?
module 1
In your module1.py
's Test
class. add a warning in your init method:
import warnings
class Test:
def __init__(self):
warnings.warn('This class is deprecated', DeprecationWarning)
This will give you the below output, where if you hover on Test
, you'll see a warning.
For that, you'll have to initialize the Test
class.
If you want to show a warning on the import statement, you can call the warning in the module1.py
file itself. However, that will happen for all your Classes in your module1.py
file if you import them.