Repository: scikit-learn/scikit-learn
File: sklearn/tests/test_base.py (Python)

Code before:
        ([1, 2], [3]),
        (np.array([1]), [2, 3]),
        (None, KFold()),
        (None, get_scorer("accuracy")),
    ],
)
def test_param_is_non_default(default_value, test_value):
    """Check that we detect non-default parameters with various types.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/31525
    """
    estimator = make_estimator_with_param(default_value)(param=test_value)
    non_default = estimator._get_params_html().non_default
    assert "param" in non_default


def test_param_is_non_default_when_pandas_NA():
    """Check that we detect pandas.Na as non-default parameter.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/32312
    """
    pd = pytest.importorskip("pandas")
    if pd:
        estimator = make_estimator_with_param(None)(param=pd.NA)
        non_default = estimator._get_params_html().non_default
        assert "param" in non_default


@pytest.mark.parametrize(
    "default_value, test_value",
    [
        (None, None),
        ((), ()),
        ((), []),
        ((), np.array([])),
        ((1, 2, 3), (1, 2, 3)),
        ((1, 2, 3), [1, 2, 3]),
        ((1, 2, 3), np.array([1, 2, 3])),
        (np.nan, np.nan),
        ("abc", "abc"),
        (True, True),
        (1, 1),
        (1.0, 1.0),
        (2, 2.0),
    ],
)
def test_param_is_default(default_value, test_value):
    """Check that we detect the default parameters and values in an array-like will
    be reported as default as well.

Review comment (bug):
As is this test doesn't fail on main because of the strange behavior of comparing against `pd.NA`. It happens that comparing against `None` does return a bool while comparing against anything else returns `NA` (which is causing the bug).
```py
pd.NA == None
# False
pd.NA == 0
# <NA>
```
```suggestion
        estimator = make_estimator_with_param(default_value=0)(param=pd.NA)
```

Code after:
        ([1, 2], [3]),
        (np.array([1]), [2, 3]),
        (None, KFold()),
        (None, get_scorer("accuracy")),
    ],
)
def test_param_is_non_default(default_value, test_value):
    """Check that we detect non-default parameters with various types.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/31525
    """
    estimator = make_estimator_with_param(default_value)(param=test_value)
    non_default = estimator._get_params_html().non_default
    assert "param" in non_default


def test_param_is_non_default_when_pandas_NA():
    """Check that we detect pandas.Na as non-default parameter.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/32312
    """
    pd = pytest.importorskip("pandas")

    estimator = make_estimator_with_param(default_value=0)(param=pd.NA)
    non_default = estimator._get_params_html().non_default
    assert "param" in non_default


@pytest.mark.parametrize(
    "default_value, test_value",
    [
        (None, None),
        ((), ()),
        ((), []),
        ((), np.array([])),
        ((1, 2, 3), (1, 2, 3)),
        ((1, 2, 3), [1, 2, 3]),
        ((1, 2, 3), np.array([1, 2, 3])),
        (np.nan, np.nan),
        ("abc", "abc"),
        (True, True),
        (1, 1),
        (1.0, 1.0),
        (2, 2.0),
    ],
)
def test_param_is_default(default_value, test_value):
    """Check that we detect the default parameters and values in an array-like will
    be reported as default as well.