Skip to content

MNT Output clearer pytest IDs for yield_namespace_device_dtype_combinations - #31192

Closed
lucyleeow wants to merge 3 commits into
scikit-learn:mainfrom
lucyleeow:fix_yield_array_api
Closed

lucyleeow wants to merge 3 commits into
scikit-learn:mainfrom
lucyleeow:fix_yield_array_api

Conversation

@lucyleeow

@lucyleeow lucyleeow commented Apr 14, 2025 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Checked ID looks okay:

sklearn/preprocessing/tests/test_label.py::test_label_encoder_array_api_compliance[y0-numpy-None-None] PASSED                                                         [  3%]
sklearn/preprocessing/tests/test_label.py::test_label_encoder_array_api_compliance[y0-array_api_strict-CPU_DEVICE-float64] PASSED                                     [  7%]
sklearn/preprocessing/tests/test_label.py::test_label_encoder_array_api_compliance[y0-array_api_strict-device1-float32] PASSED 

What does this implement/fix? Explain your changes.

Any other comments?

cc @lesteve @ogrisel

@github-actions

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: dfef507. Link to the linter CI: here

@lesteve lesteve left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

While you are at it, maybe use the same id in the except ImportError clause a few lines below? I think right now they are roughly the same, but I guess it would be nice to use a more similar code so that the name of the test is the same whether the test is skipped or not.

@lucyleeow

lucyleeow commented Apr 16, 2025 •

Copy link
Copy Markdown
Member Author

So tests are failing because when we use yield_namespace_device_dtype_combinations outside of pytest parametrizations, in:

def _yield_array_api_checks(estimator):
for (
array_namespace,
device,
dtype_name,
) in yield_namespace_device_dtype_combinations():

we yield a ParameterSet object (https://docs.pytest.org/en/stable/reference/reference.html#pytest.param) (for the array api strict case, and a tuple in other cases). ParameterSet happens to include 3 items, just not the 3 we 'expected': values, marks and id;

In [15]: a,b,c = pytest.param('a', 'b', 'c', marks="m", id='test')

In [16]: a
Out[16]: ('a', 'b', 'c')

In [17]: b
Out[17]: 'm'

In [18]: c
Out[18]: 'test'

So I think we can amend all yield values in yield_namespace_device_dtype_combinations to be a pytest param object, and I can update _yield_array_api_checks to deal with ParameterSet object instead. WDYT?

@thomasjpfan

Copy link
Copy Markdown
Member

So I think we can amend all yield values in yield_namespace_device_dtype_combinations to be a pytest param object, and I can update _yield_array_api_checks to deal with ParameterSet object instead. WDYT?

I'm okay with this.

@lesteve

lesteve commented Apr 16, 2025 •

Copy link
Copy Markdown
Member

It seemed like a nice idea, but unfortunately the real world can be somewhat mean to nice ideas 😅, maybe it is better after all to forget about it?

It feels like the status quo of having to remember to use ids=_get_namespace_device_dtype_ids is not that bad. My feeling is that people will copy and paste from existing tests and everything will be mostly fine.

So I think we can amend all yield values in yield_namespace_device_dtype_combinations to be a pytest param object, and I can update _yield_array_api_checks to deal with ParameterSet object instead. WDYT?

Tricky ... I think there is also an additional constraint that I don't think we want to have a hard dependency on pytest if you only import stuff from sklearn modules.

For example, check_estimator is supposed to be run without pytest installed see

def _maybe_mark(
estimator,
check,
expected_failed_checks: dict[str, str] | None = None,
mark: Literal["xfail", "skip", None] = None,
pytest=None,
):
"""Mark the test as xfail or skip if needed.
Parameters
----------
estimator : estimator object
Estimator instance for which to generate checks.
check : partial or callable
Check to be marked.
expected_failed_checks : dict[str, str], default=None
Dictionary of the form {check_name: reason} for checks that are expected to
fail.
mark : "xfail" or "skip" or None
Whether to mark the check as xfail or skip.
pytest : pytest module, default=None
Pytest module to use to mark the check. This is only needed if ``mark`` is
`"xfail"`. Note that one can run `check_estimator` without having `pytest`
installed. This is used in combination with `parametrize_with_checks` only.
"""
.

Not sure whether we are testing this thoroughly though because I would have expected a failure somewhere on this PR. I guess we don't have a build with both array_api_strict installed and CHECK_PYTEST_SOFT_DEPENDENCY?

@lucyleeow

Copy link
Copy Markdown
Member Author

I hadn't thought about the import, that's a good point.

I guess we don't have a build with both array_api_strict installed and CHECK_PYTEST_SOFT_DEPENDENCY

I added a pytest import to the top of _array_api.py, so you'd think when we do:

if tags.array_api_support:
for check in _yield_array_api_checks(estimator):
yield check

we'd import pytest, so even without array_api_strict installed, we'd end up importing pytest?

Happy to proceed either way.

@thomasjpfan

thomasjpfan commented Apr 17, 2025 •

Copy link
Copy Markdown
Member

we'd import pytest, so even without array_api_strict installed, we'd end up importing pytest?

I would remove the pytest import from sklearn/utils/_array_api.py. pytest should not be imported during runtime.

Another other option is to a use a fixture:

# conftest.py
# it's safe to use `pytest` features within `conftest.py`
def _yield_namespace_device_dtype_combinations_with_id():
    for item in yield_namespace_device_dtype_combinations():
         yield pytest.param(item, id=_get_id(item))

@pytest.fixture(params=_yield_namespace_device_dtype_combinations_with_id())
def namespace_device_case(request):
    return request.param
   

# in test_*.py
def test_something(namespace_device_case):
    array_namespace, device, dtype_name = namespace_device_case

@lesteve

lesteve commented Apr 17, 2025 •

Copy link
Copy Markdown
Member

Could work I guess? But I mean, I would still be slightly in favour of the dumb option: copy around two lines instead of one each time I write an array api test 😅.

There also seems to be an issue with our test for checking soft dependency on pytest but I am certainly not planning to look at it in the near future.

@thomasjpfan

Copy link
Copy Markdown
Member

But I mean, I would still be slightly in favour of the dumb option: copy around two lines instead of one each time I write a array api test 😅.

I'm okay with copying everywhere as well.

@lucyleeow

Copy link
Copy Markdown
Member Author

Thanks all. I am happy with the 'dumb' option too. I will close this and re-open the other PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants