Skip to content

Introduce --nonp to error when NumPy is accessed #76

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 1 commit into from
Mar 2, 2023

Conversation

honno
Copy link
Contributor

@honno honno commented Mar 2, 2023

Using --nonp will error before any tests are run when NumPy is accessed. I think this is especially useful as we can use it iteratively—

$ cat torch_np/tests/test_foo.py
def test_foo:
    assert True
$ pytest torch_np/tests/test_foo.py
# no error

No error is raised because the tests pytest has collected never import NumPy. Meanwhile

$ cat torch_np/_uses_numpy.py
import numpy as np
...
np.int8
...
$ cat torch_np/tests/test_bar.py
from torch_np import _uses_numpy
$ pytest torch_np/tests/test_bar.py
...
ERROR torch_np/tests/test_bar.py - RuntimeError: Using --nonp but accessed np.int8
    torch_np/tests/test_bar.py:1: in <module>
        ???
    torch_np/_uses_numpy.py:4: in <module>
        ???
    torch_np/tests/conftest.py:20: in __getattribute__
        ???
    E   RuntimeError: Using --nonp but accessed np.int8
...
ERROR torch_np/tests/test_bar.py - RuntimeError: Using --nonp but accessed np.int8

i.e. only when the test ends up having NumPy imported does it error on the collection stage. This means we can leverage the coverage of individual test files and work on getting those passing before jumping on to another, til eventually just pytest torch_np/tests/ --nonp passes (and then we can introduce a CI job for it to prevent regressions).

@honno honno requested a review from ev-br March 2, 2023 11:28
@ev-br ev-br merged commit 1272d80 into Quansight-Labs:main Mar 2, 2023
@ev-br
Copy link
Collaborator

ev-br commented Mar 2, 2023

This looks slick, wow :-)

@honno honno deleted the nonp branch March 2, 2023 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants