Python警告过滤器未隐藏此弃用警告

0 投票
1 回答
49 浏览
提问于 2025-04-12 19:22

即使切换到 mstrio.project_objects.dashboard,仍然会出现弃用警告,而且忽略这个警告也没有用。

$ pip install -Uq mstrio-py
$ export PYTHONWARNINGS='ignore:mstrio.project_objects.dossier module is deprecated:DeprecationWarning'
$ python3 -c 'from mstrio.project_objects import dashboard'
DeprecationWarning: mstrio.project_objects.dossier module is deprecated and will not be supported starting from mstrio-py 11.5.03. Please use mstrio.project_objects.dashboard instead.
$ python3 -W "ignore:mstrio.project_objects.dossier module is deprecated:DeprecationWarning" -c "from mstrio.project_objects import dashboard"
DeprecationWarning: mstrio.project_objects.dossier module is deprecated and will not be supported starting from mstrio-py 11.5.03. Please use mstrio.project_objects.dashboard instead.

用上下文管理器来保护导入也不管用。

>>> import warnings
>>> with warnings.catch_warnings():
...     warnings.filterwarnings("ignore", message=".*mstrio.project_objects.dossier module is deprecated.*")
...     from mstrio.project_objects import dashboard
... 
DeprecationWarning: mstrio.project_objects.dossier module is deprecated and will not be supported starting from mstrio-py 11.5.03. Please use mstrio.project_objects.dashboard instead.

为什么这个警告还在呢?怎么才能避免这个弃用警告呢?

这是最新的 mstrio-py 版本,也就是 11.4.3.101

1 个回答

1

mstrio覆盖你的警告过滤器

warnings.filterwarnings(action=print_warnings, module=module_path)
warnings.filterwarnings(
    action=print_warnings, category=DeprecationWarning, module=module_path
)
warnings.filterwarnings(action='default', category=UserWarning, module=module_path)

它还会 修改警告信息的格式,所以你看到的警告信息和通常的样子不一样:

def custom_formatwarning(msg, category, *args, **kwargs):
    # ignore everything except the message
    return str(category.__name__) + ': ' + str(msg) + '\n'


warnings.formatwarning = custom_formatwarning

那个文件里有很多内容,其实这个包 根本不应该 这样做。你最好的办法可能是向 MicroStrategy 提出一个问题,请他们来修改这个。

撰写回答