如何在Python Hypothesis中禁用NonInteractiveExampleWarning
禁用Hypothesis的NonInteractiveExampleWarning警告
有三种简便的方法可以满足你的需求:
方法一:用Python标准库过滤特定警告
直接借助warnings模块精准屏蔽该警告,不影响其他警告输出:
import warnings from hypothesis import strategies as st from hypothesis.errors import NonInteractiveExampleWarning # 忽略指定类型的警告 warnings.filterwarnings("ignore", category=NonInteractiveExampleWarning) ints = st.integers() ints.example()
方法二:通过环境变量全局禁用
无需修改代码,在运行脚本前设置环境变量即可:
- Linux/macOS终端执行:
export HYPOTHESIS_SUPPRESS_WARNINGS=1 python your_script.py
- Windows命令提示符执行:
set HYPOTHESIS_SUPPRESS_WARNINGS=1 python your_script.py
方法三:使用Hypothesis配置API
通过Hypothesis自身的配置接口注册配置文件,全局生效:
from hypothesis import settings from hypothesis.errors import NonInteractiveExampleWarning # 注册并加载自定义配置,屏蔽目标警告 settings.register_profile( "non_interactive", suppress_warnings=[NonInteractiveExampleWarning] ) settings.load_profile("non_interactive") from hypothesis import strategies as st ints = st.integers() ints.example()
内容的提问来源于stack exchange,提问作者Hatatister
相关产品推荐
相关产品推荐

