7

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 contains -

class Test:
    pass

module 2 contains -

from module1 import Test #this line should show a DeprecationWarning.
sophros
  • 14,672
  • 11
  • 46
  • 75
Nirmal
  • 116
  • 1
  • 4
  • Although common sense dictates it by default, you should probably make the fact that you want to be able to use the class, explicit. – CristiFati Mar 02 '20 at 11:22
  • would this be a good subject matter for meta classes? on the`__init__` side o things you only get it with a never seen before class. would this be a good use of*if-you-gotta-ask-aint-needin* metaclassses? – JL Peyret Mar 04 '20 at 07:46

4 Answers4

4

The import will execute the class definition, so put the warning there:

class Test:
    raise DeprecationWarning('This class is deprecated')
Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
  • With my current implementation, the __init__ method of class Test calls a method of a different class. I want to keep that behavior with a warning for now, due to some dependencies. – Nirmal Mar 02 '20 at 09:52
  • I'm not quite sure what you mean. Of course, you can also raise a warning in `__init__` if you prefer. Then you would get a warning when making an instance of the class, but not on import. – Jussi Nurminen Mar 02 '20 at 09:57
  • It should be noted that `import` happens across the entire module indiscriminately, so whether you did `from module1 import Test`, `import module1`, or `from module1 import OtherClass`, it always yields the same result. – Ondrej K. Mar 02 '20 at 10:48
  • If you import more than once, does it show the message twice? Against the *importing* module? It'll be in `sys.modules`. Good idea tho. – JL Peyret Mar 04 '20 at 05:10
1

You could use [Python 3.Docs]: warnings - Warning control, which states (emphasis is mine):

Changed in version 3.2: DeprecationWarning is now ignored by default in addition to PendingDeprecationWarning.

so, you'll have to "manually" enable it, otherwise it won't be visible when importing mod00. This way:

  • The warning will be displayed when importing or executing the module
  • The class can be instantiated as well

mod00.py:

#!/usr/bin/env python

import warnings
warnings.filterwarnings("default", category=DeprecationWarning, module=__name__)

print("Module mod00")


class Dummy:
    warnings.warn("Dummy class is deprecated", category=DeprecationWarning, stacklevel=2)


if __name__ == "__main__":
    print("Execute module mod00")

mod01.py:

#!/usr/bin/env python

from mod00 import Dummy


if __name__ == "__main__":
    print("Execute module mod01")
    dummy = Dummy()
    print(dummy)

Output:

e:\Work\Dev\StackOverflow\q060486000>sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" -m mod00
Module mod00
e:\Work\Dev\StackOverflow\q060486000\mod00.py:9: DeprecationWarning: Dummy class is deprecated
  class Dummy:
Execute module mod00

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" mod00.py
Module mod00
mod00.py:9: DeprecationWarning: Dummy class is deprecated
  class Dummy:
Execute module mod00

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" -c "import mod00"
Module mod00
e:\Work\Dev\StackOverflow\q060486000\mod00.py:9: DeprecationWarning: Dummy class is deprecated
  class Dummy:

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" -m mod01
Module mod00
e:\Work\Dev\StackOverflow\q060486000\mod00.py:9: DeprecationWarning: Dummy class is deprecated
  class Dummy:
Execute module mod01
<mod00.Dummy object at 0x000001EBBC685488>
CristiFati
  • 38,250
  • 9
  • 50
  • 87
-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.

enter image description here

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.

enter image description here

Harsh Mehta
  • 249
  • 1
  • 10
-3

Add this code to your index page:

error_reporting(1);
ini_set('display_startup_errors', 0);
King Tyga
  • 31
  • 6