Skip to content

BUG: fix importing numpy in Python's optimized mode - #27868

Merged
rgommers merged 3 commits into
numpy:mainfrom
neutrinoceros:bug/fix_importing_numpy_with_OO
Nov 28, 2024
Merged

rgommers merged 3 commits into
numpy:mainfrom
neutrinoceros:bug/fix_importing_numpy_with_OO

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Follow up to gh-27490, fix gh-27866

Comment thread numpy/_core/overrides.py Outdated
@@ -21,6 +22,11 @@

def get_array_function_like_doc(public_api, docstring_template=None):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I realized that changing the default value for docstring_template to an empty string would probably work too, but I cannot test it right now (afk).

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Nov 27, 2024
@charris charris added this to the 2.2.0 release milestone Nov 27, 2024
@neutrinoceros
neutrinoceros marked this pull request as draft November 27, 2024 13:19

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good but perhaps good to follow #27866 (comment) and also change a CI test to ensure this does not recur.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

sure thing. What for would be appropriate ? Do we want to run the whole test suite or just try python -OO -c 'import numpy' ? and where should this new job live ?

@rgommers

Copy link
Copy Markdown
Member

The whole test suite, and no new job needed I think - just adding PYTHONOPTIMIZE=2 to the existing full job at https://github.com/numpy/numpy/blob/main/.github/workflows/linux.yml#L153 should cover our bases.

@neutrinoceros
neutrinoceros force-pushed the bug/fix_importing_numpy_with_OO branch from 9211175 to c1166d5 Compare November 27, 2024 14:28
@neutrinoceros
neutrinoceros marked this pull request as ready for review November 27, 2024 14:29
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Interesting, this found one failing test under PYTHONOPTIMIZE=2 . Let me try to fix this too.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Only the test itself didn't support optimized mode because it relied on having IO operations evaled through assert statements. Separating evaluation from assertion should do the trick (it works locally, at least).

@mhvk

mhvk commented Nov 27, 2024

Copy link
Copy Markdown
Contributor

Does this mean that all the assert in the tests are actually removed? I.e., that full now only tests that the tests run, but not actually that the output is correct? That may not be horrible, but perhaps not quite intended either... Especially as surely there are other tests whether something is calculated inside the assert, and thus coverage may be incomplete too.

I wonder if we can pick a shorter run and just do tests twice, as in

pytest ...
PYTHONOPTIMIZE=2 pytest ...

(Checking this locally on another project, I get a failure because astropy can also not be loaded with -O...)

Alternatively, perhaps we do need a special branch that just imports everything?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Does this mean that all the assert in the tests are actually removed?

yes. That is a direct expectation from running python with PYTHONOPTIMIZE >0

@rgommers

Copy link
Copy Markdown
Member

Wasn't that fixed? IIRC that was a main reason for us avoiding plain assert in the past (in favor of our own np.testing.assert_), and pytest got robust against that at some point I believe.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Oh yes, sorry, I see from doing more experiments with it locally that actually, only assert statements that are not part of tests are expected to be skipped. In fact, pytest warns about it

PytestConfigWarning: assertions not in test modules or plugins will be ignored because assert statements are not executed by the underlying Python interpreter (are you using python -O?)

though, I don't understand why that one test needed tweaking...

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It was the test tweak that made me wonder - I also thought this was fixed. Just to be sure, I also confirmed that a failing test actually fails with optimization on.

Anyway, chalk it up to the mysteries of python...

@mhvk

mhvk commented Nov 27, 2024

Copy link
Copy Markdown
Contributor

@seberg

seberg commented Nov 28, 2024 •

Copy link
Copy Markdown
Member

My guess is that this is due to:

numpy/_core/tests/test_deprecations.py:        from numpy.lib.tests.test_io import TextIO

i.e. the module is already imported before pytest does its magic. I think it would be nice if pytest would warn (or even error) but dunno if that is doable.

Maybe open an issue there?

So an alternative fix would be moving the TextIO use (or seeing if the deprecation test is even worth it).
EDIT: In fact, it is probably fine to always warn if a test file imports another test file...

But, since that seems the reason, this work-around is also not a big deal.

EDIT: To be clear, the full file has no asserts in this test run. But, it is also only this test file that seems to be imported anywhere.

@rgommers rgommers 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.

This seems to do the job, so while we're pondering the pytest magic related hiccups, I'll go ahead and merge this and rebuild the nightly wheels to avoid more issues downstream (the failing SciPy CI job needs a local fix though, since it currently pulls in numpy 2.2.0rc1).

@rgommers

Copy link
Copy Markdown
Member

Thanks @neutrinoceros, @mhvk and @seberg!

@rgommers
rgommers merged commit 63503c9 into numpy:main Nov 28, 2024
@neutrinoceros
neutrinoceros deleted the bug/fix_importing_numpy_with_OO branch November 28, 2024 11:09
charris pushed a commit to charris/numpy that referenced this pull request Nov 28, 2024
* TST: add PYTHONOPTIMIZE=2 to full CI (linux)

* BUG: fix importing numpy in Python's optimized mode

* TST: fix a test for compatibility with PYTHONOPTIMIZE
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 28, 2024
charris added a commit that referenced this pull request Nov 28, 2024
BUG: fix importing numpy in Python's optimized mode (#27868)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: NumPy 2.2.0rc1 import fails with PYTHONOPTIMIZE=2

5 participants