Skip to content
Advertisement

Pytest: How to locate a FutureWarning and fix it?

In my current project when I run my tests (with pytest) I get this output (besides others):

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 "

This FutureWarning is quite clear – from version 1.0 (I’m using 0.24 at the moment) there will be change to some parameters and I will not be able to pass standardize as positional arguments.

But the question is – How can I locate where should I change this positional argument to a keyword one?

I could go to validation.py that is mentioned in the warning output, but there is general _deprecate_positional_args function which does not tell me which code call it.

In this particular case I’m guessing this will come from PowerTransformer imported from sklearn.preprocessing which is used in my code, has standardize parameter and is tested in the listed tests. Is there a general way how to find the cause of that easily for any FutureWarning? Or do I need to check all my codes and try to find it somehow?

Advertisement

Answer

Pytest let you raise a FutureWarning as an error, this way it points to the code location where the warning was raised, the simplest way is through the warning flag

pytest -W error::FutureWarning test_script.py

or inside test_script.py through pytestmark

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

you can read more about specifying the action being taken on a Warning here and here.

Example

Considering the following dummy example test_transform.py script

# 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

and running on terminal

pytest test_transform.py > logs.log

the logs show me exactly that PowerTransformer raised the 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 ======================================================

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement