gpt4 book ai didi

python - Pytest:如何找到 FutureWarning 并修复它?

转载 作者:行者123 更新时间:2023-12-04 16:45:45 26 4
gpt4 key购买 nike

在我当前的项目中,当我运行测试(使用 pytest )时,我得到了这个输出(除了其他):

ml_framework/tests/test_impute.py: 8 warnings
ml_framework/tests/test_transform_pipeline.py: 9 warnings
ml_framework/tests/test_data_transforms.py: 27 warnings
ml_framework/tests/test_jsonizer.py: 4 warnings
/home/sondracek/anaconda3/envs/p3.8/lib/python3.8/site-packages/sklearn/utils/validation.py:70: FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error
warnings.warn(f"Pass {args_msg} as keyword args. From version "
FutureWarning很清楚 - 从 1.0 版开始(我现在正在使用 0.24),某些参数会发生变化,我将无法通过 standardize作为位置参数。
但问题是 - 我如何找到应该在哪里将此位置参数更改为关键字一?
我可以去 validation.py警告输出中提到的,但有一般 _deprecate_positional_args没有告诉我哪个代码调用它的函数。
在这种特殊情况下,我猜这将来自 PowerTransformer进口自 sklearn.preprocessing在我的代码中使用的有 standardize参数并在列出的测试中进行测试。 有没有一种通用的方法可以轻松找到任何 FutureWarning 的原因? ? 或者我是否需要检查我的所有代码并尝试以某种方式找到它?

最佳答案

Pytest 让你养FutureWarning作为错误,这样它指向引发警告的代码位置,最简单的方法是通过警告标志

pytest -W error::FutureWarning test_script.py
或内部 test_script.py通过 pytestmark
pytestmark = pytest.mark.filterwarnings("error::FutureWarning")
您可以阅读有关指定对警告采取的操作的更多信息 herehere .
示例
考虑以下虚拟示例 test_transform.py脚本
# test_transform.py
import pytest


pytestmark = pytest.mark.filterwarnings("error::FutureWarning")


def test_transformer_boxcox():

from sklearn.preprocessing import PowerTransformer

pt = PowerTransformer('box-cox', False)

assert True


def test_transformer_yeo_johnson():

from sklearn.preprocessing import PowerTransformer

pt = PowerTransformer('yeo-johnson', False)

assert True
并在终端上运行
pytest test_transform.py > logs.log
日志准确地告诉我 PowerTransformer提出了 FutureWarning .
===================================================== test session starts =====================================================
platform linux -- Python 3.7.6, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/jovyan/work
collected 2 items

test_transform.py FF [100%]

========================================================== FAILURES ===========================================================
___________________________________________________ test_transformer_boxcox ___________________________________________________

def test_transformer_boxcox():

from sklearn.preprocessing import PowerTransformer

> pt = PowerTransformer('box-cox', False)

test_transform.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bda14d50>, 'box-cox', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'

@wraps(f)
def inner_f(*args, **kwargs):
extra_args = len(args) - len(all_args)
if extra_args <= 0:
return f(*args, **kwargs)

# extra_args > 0
args_msg = ['{}={}'.format(name, arg)
for name, arg in zip(kwonly_args[:extra_args],
args[-extra_args:])]
args_msg = ", ".join(args_msg)
warnings.warn(f"Pass {args_msg} as keyword args. From version "
f"{version} passing these as positional arguments "
> "will result in an error", FutureWarning)
E FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error

/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
________________________________________________ test_transformer_yeo_johnson _________________________________________________

def test_transformer_yeo_johnson():

from sklearn.preprocessing import PowerTransformer

> pt = PowerTransformer('yeo-johnson', False)

test_transform.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bd02f750>, 'yeo-johnson', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'

@wraps(f)
def inner_f(*args, **kwargs):
extra_args = len(args) - len(all_args)
if extra_args <= 0:
return f(*args, **kwargs)

# extra_args > 0
args_msg = ['{}={}'.format(name, arg)
for name, arg in zip(kwonly_args[:extra_args],
args[-extra_args:])]
args_msg = ", ".join(args_msg)
warnings.warn(f"Pass {args_msg} as keyword args. From version "
f"{version} passing these as positional arguments "
> "will result in an error", FutureWarning)
E FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error

/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
=================================================== short test summary info ===================================================
FAILED test_transform.py::test_transformer_boxcox - FutureWarning: Pass standardize=False as keyword args. From version 1.0 ...
FAILED test_transform.py::test_transformer_yeo_johnson - FutureWarning: Pass standardize=False as keyword args. From version...
====================================================== 2 failed in 0.71s ======================================================

关于python - Pytest:如何找到 FutureWarning 并修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66900236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com