我试图将一个函数标记为已弃用,以便调用它的脚本运行正常完成,但被 PyCharm 的静态代码检查捕获。 (关于此弃用警告还有一些其他问题,但我认为它们早于 Python 2.6,当时我相信引入了基于类的异常。)
这是我所拥有的:
class Deprecated(DeprecationWarning):
pass
def save_plot_and_insert(filename, worksheet, row, col):
"""
Deprecated. Docstring ...<snip>
"""
raise Deprecated()
# Active lines of
# the function here
# ...
我的理解是 Deprecated Warnings 应该允许代码运行,但是这个代码示例实际上在调用函数时停止。当我从函数体中删除“raise”时,代码会运行,但 PyCharm 不会将函数调用标记为已弃用。
将函数标记为已弃用的 Pythonic (2.7.x) 方式是什么?
最佳答案
你不应该raise DeprecationWarning
(或子类),因为那时您仍在引发实际异常。
而是使用 warnings.warn
:
import warnings
warnings.warn("deprecated", DeprecationWarning)
关于python-2.7 - 如何在 Python 2.7 中引发弃用警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378505/