当我运行 pytest 时,我收到了来自 3rd 方库的一些弃用警告。我想知道我自己代码中的任何弃用警告,而不是与另一个 3rd 方库捆绑在一起的库的供应商副本。
这个答案有助于让我中途到达那里。如果我像这样运行 pytest:
$ pytest ./tests/我得到:
$ pytest ./tests/
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items
tests/test_file1.py . [ 20%]
tests/test_file2.py .... [100%]
=============================== warnings summary ===============================
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Mapping, MutableMapping
-- Docs: https://docs.pytest.org/en/latest/warnings.html …Run Code Online (Sandbox Code Playgroud) 根据Python警告文档, filterwarnings模式是action:message:category:module:line,并且该module部分应该允许根据正则表达式匹配特定模块。我试图让这项工作来过滤第三方库模块的特定警告,但该功能似乎不起作用。
最小的例子
重现问题的更简单方法是创建一个文件
my_module.py
print("\d")
Run Code Online (Sandbox Code Playgroud)
然后PYTHONWARNINGS="error,ignore:::.*" python -c "import my_module"从同一目录运行。显然,正则表达式.*应该与模块名称匹配my_module,但不知何故却没有。问题是为什么?
原始示例
举个例子:导入包(机器人操作系统(ROS)rosbag发行版附带的一些包)在“严格”report-warnings-as-errors 模式下运行时会触发错误:
print("\d")
Run Code Online (Sandbox Code Playgroud)
这是有道理的,它应该使用原始字符串文字。
现在我尝试使用模块正则表达式来消除来自rosbag模块的警告。出于示例的目的,我通过指定过滤器警告,PYTHONWARNINGS但其他方法(例如通过 pytests 设置)会导致相同的行为。
我收到以下意外行为:
PYTHONWARNINGS='error,ignore:invalid escape sequence::'作品。当然,这会过滤所有“无效转义序列”警告。因此,让我们module在末尾添加一个正则表达式以特定于模块......PYTHONWARNINGS='error,ignore:invalid escape sequence::rosbag.*'应该完全做到这一点,但过滤器似乎不匹配。警告仍然被报告为错误,就像普通的一样error。PYTHONWARNINGS='error,ignore:::.*'理论上应该匹配任何模块的 using 也无法匹配。仅像匹配一样完全删除正则表达式PYTHONWARNINGS='error,ignore:::',但当然这本质上相当于只是PYTHONWARNINGS='ignore'.任何想法为什么模块正则表达式根本不匹配?!这是 Python 发行版的错误吗?我使用的是 Python 3.8.10 / Ubuntu 20.04。