Repository navigation
Conversation
lucascolley
left a comment
There was a problem hiding this comment.
LGTM! Happy with the cast stuff
I wonder if a CI job should be added that runs the Python interpreter with -O. This will trigger a warning from pytest that currently gets turned into an error. SciPy currently filters this warning in its pytest.ini (scipy/scipy@f46f55f/pytest.ini#L25).
happy to add one, our CI is pretty fast
(if you're wanting to do that here, or a follow-up is fine) |
Thanks. I'll do it here. |
| environment: | ||
| - tests-py312 | ||
| - tests-py314 | ||
| - tests-numpy1 | ||
| - tests-nogil | ||
| - tests-backends | ||
| - tests-backends-py312 | ||
| - tests-run-deps |
There was a problem hiding this comment.
it would be better to avoid adding a whole new job and instead add to the matrix here. Try adding a new Pixi env with activation.env.PYTHONOPTIMIZE = 1 and listing it here
|
thanks, as a nit I would rename the specific 'asserts-removed' to the more general 'python-optimized'. Otherwise LGTM |
Thanks! Done |
Reference issue
Closes #1068
What does this implement/fix?
This replaces the use of
assertstatements inarray_api_extra.testingwith a helper that branches on the condition and directly raises anAssertionErrorif it holds. This is done so that these checks will still work even if the CPython interpreter is run with-O,-OOor with the environment variablePYTHONOPTIMIZEset to a value greater than or equal to1.asserts are stripped out when the interpreter is run in such a way.I found that NumPy also takes care to avoid direct uses of
assertinnumpy.testingand have adapted the helper from https://numpy.org/doc/stable/reference/generated/numpy.testing.assert_.html, but had to change it a little to work witharray_api_extra's more strict linting and type checking.Additional information
I had to flip the order of some casts and checks at one point because the linter complained. This seems to have been a pre-existing issue that was grandfathered in prior to the strict checks being set up. There are assertions a value is not None where the value couldn't possibly be
Noneif the casts are correct.I wonder if a CI job should be added that runs the Python interpreter with
-O. This will trigger a warning from pytest that currently gets turned into an error. SciPy currently filters this warning in itspytest.ini(https://github.com/scipy/scipy/blob/f46f55fc2ba8f81239494b40f2f3a3d844476650/pytest.ini#L25).AI Generation Disclosure
I used GPT 6.1 Sol in a chat to help debug the mysterious failure in scipy/scipy#26512 and to review my work. The LLM identified that the problem was due to
PYTHONOPTIMIZEbeing set in the failing CI job. It was my idea to replaceassertstatements with a helper function and I found the prior art innumpy.testing.assert_while investigating why things fromnumpy.testingdon't cause the failures we saw on scipy/scipy#26512. The LLM was the one that suggested the need to flip the order of some assertions and casts, so that transposition can be considered AI generated 😎