



















When we are using 3rd packages in Python, we may see some warning messages. For example, when you use the package BeautifulSoup and provide it with an url:
import bs4
def use_bs():
bs4.BeautifulSoup('https://google.com')
if __name__ == "__main__":
use_bs()you will see the warning below:
MarkupResemblesLocatorWarning: The input looks more like a URL than markup. You may want to use an HTTP client like requests to get the document
behind the URL, and feed that document to Beautiful Soup.
bs4.BeautifulSoup('https://google.com').txtIn this post, I am not going to discuss whether you should suppress this warning. It is discussed already here and here. I only want to summarize how you can actually disable the warning if you want.
In normal python code, you can use the warnings.filterwarnings method from the Python standard library.
import warnings
import bs4
warnings.filterwarnings(
action='ignore',
category=bs4.MarkupResemblesLocatorWarning
)
def use_bs():
bs4.BeautifulSoup('https://google.com')
if __name__ == "__main__":
use_bs()However, even if you disable the warning in your code, when you import the module and run test with pytest, the warning will still be shown. That is because pytest has its own mechanism to deal with the warnings.
To ignore warnings, we can set up the filterwarnings option for pytest in pyproject.toml:
[tool.pytest.ini_options]
filterwarnings = [
"ignore::bs4.MarkupResemblesLocatorWarning",
]The above warning filter format is from the standard warning package:
# note the module here refers to the module that is producing the warning itself,
# not calling module.
action:message:category:module:lineWhat is worth noting is that, when you specify the category that is python builtin, you need to specify the full path. Otherwise, pytest is trying import the warning from Python builtin warning, and will result in an error:
ERROR: while parsing the following warning configuration:
ignore::MyWarning
This error occurred:
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1690, in parse_warning_filter
category: Type[Warning] = _resolve_warning_category(category_)
File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1729, in _resolve_warning_category
cat = getattr(m, klass)
AttributeError: module 'builtins' has no attribute 'MyWarning'. Did you mean: 'Warning'?The same issue has also been described in this post.
Also note that due to the timing of how pytest adds the current working directory to sys.path,
if you use category for a warning filter, you might still experience error when pytest tries to parse the filter.
See the problem describe here.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。