在python中导入类时显示警告

Nir*_*mal 6 python warnings exception deprecation-warning

我在不同的模块中有一个类,我想在DeprecationWarning导入该类时显示一个。这样做的正确方法是什么?

模块 1 包含 -

class Test:
    pass
Run Code Online (Sandbox Code Playgroud)

模块 2 包含 -

from module1 import Test #this line should show a DeprecationWarning.
Run Code Online (Sandbox Code Playgroud)

Jus*_*nen 4

导入将执行类定义,因此将警告放在那里:

class Test:
    raise DeprecationWarning('This class is deprecated')
Run Code Online (Sandbox Code Playgroud)