[英]Pytest: How to locate a FutureWarning and fix it?
在我當前的項目中,當我運行測試(使用pytest
)時,我得到了這個 output (除其他外):
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
作為位置 arguments 傳遞。
但問題是 -我怎樣才能找到我應該在哪里將此位置參數更改為關鍵字?
我可以 go 到警告 output 中提到的validation.py
,但是有一般_deprecate_positional_args
function 不告訴我哪個代碼調用它。
在這種特殊情況下,我猜這將來自從我的代碼中使用的sklearn.preprocessing
導入的PowerTransformer
,具有standardize
參數並在列出的測試中進行了測試。 是否有一種通用方法可以輕松找到任何FutureWarning
的原因? 還是我需要檢查所有代碼並嘗試以某種方式找到它?
Pytest 讓您將FutureWarning
作為錯誤引發,這樣它指向引發警告的代碼位置,最簡單的方法是通過警告標志
pytest -W error::FutureWarning test_script.py
或通過pytestmark在test_script.py
中
pytestmark = pytest.mark.filterwarnings("error::FutureWarning")
您可以在此處和 此處閱讀有關指定對警告采取的操作的更多信息。
例子
考慮以下虛擬示例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 ======================================================
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.