Skip to content

MNT Use pytest --import-mode=importlib #31209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2025

Conversation

lesteve
Copy link
Member

@lesteve lesteve commented Apr 15, 2025

This would get pytest assertion rewriting back with meson editable install after @fcharras's investigation of mesonbuild/meson-python#646.

--import-mode=importlib is what pytest recommends see their doc. The default prepend is an historical thing that they don't recommend and they probably don't want to change the default to not break backward-compatibility.

The recommendation was already the case for our minimum pytest version see 7.1.x doc.

This seems to be working fine locally, let's see what the CI has to say about it.

Copy link

github-actions bot commented Apr 15, 2025

✔️ Linting Passed

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

Generated for commit: 9e4fb58. Link to the linter CI: here

@lesteve
Copy link
Member Author

lesteve commented Apr 15, 2025

So the pytest assertion rewriting seems to work in all CI with this change I added a broken test on purpose see build log

_________________________________ test_broken __________________________________
[gw0] linux -- Python 3.13.3 /usr/share/miniconda/envs/testvenv/bin/python

    def test_broken():
>       assert 2 + 1 == 10
E       assert (2 + 1) == 10

Without --import-mode=importlib there is no detail on AssertionError:

_________________________________________________________________________________________________________________ test_broken __________________________________________________________________________________________________________________

    def test_broken():
>       assert 2 + 1 == 10
E       AssertionError

sklearn/tests/test_isotonic.py:712: AssertionError

Note that in my broken test the additional info is not very useful but in general can be a lot more useful e.g. something like this:

_________________________________________________________________________________________________________________ test_broken __________________________________________________________________________________________________________________

    def test_broken():
        X = np.arange(10).reshape(2, 5)
>       assert X.sum() == 10
E       assert np.int64(45) == 10
E        +  where np.int64(45) = <built-in method sum of numpy.ndarray object at 0x795048da1590>()
E        +    where <built-in method sum of numpy.ndarray object at 0x795048da1590> = array([[0, 1, 2, 3, 4],\n       [5, 6, 7, 8, 9]]).sum

sklearn/tests/test_isotonic.py:713: AssertionError

An historical case where pytest assertion rewriting was useful: #30315 (comment). An example where it was annoying not to have it: #29253 (comment).

@lesteve lesteve added the Quick Review For PRs that are quick to review label Apr 15, 2025
@lesteve lesteve marked this pull request as ready for review April 15, 2025 17:29
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

LGTM. I was wondering if the asserts were always like this but then I thought it was my memory playing with me. It was not apprently :) thx for the fix

@jeremiedbb jeremiedbb merged commit 1ed6943 into scikit-learn:main Apr 15, 2025
64 checks passed
@lesteve lesteve deleted the pytest-import-mode-importlib branch April 16, 2025 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Quick Review For PRs that are quick to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants