Skip to content
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

Fix numpy doctest #176

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

floesche
Copy link
Contributor

@floesche floesche commented Dec 11, 2024

This PR potentially improves the numpy>=2.0 compatibility

  • I replaced two references to np.float_ as a dtype (and the minimum numpy version 1.16 seems to support both)
  • NEP51 changes the representation of scalars (e.g. np.float64(1.074), which fails the doctest when compared to 1.074). While numpy<2.0 is still an accepted dependency, I change the representation back to python scalars to pass the tests.

This problem came up in #175, but is a separate issue, hence the separate PR.

replace np.float_ with np.float64. The minimum numpy version 1.16 has
both as aliases, the newer version numpy>=2.0 don't support np.float_
anymore.
[NEP51](https://numpy.org/neps/nep-0051-scalar-representation.html#backward-compatibility)
changes the representation of scalars. While the long term strategy
should be a comparison with the new form of representation, the
discussion in [issue
24470](numpy/numpy#24470) seems to suggest to
use an intermediate step while both, numpy-1 and numpy-2 are supported.
Copy link
Collaborator

@schlegelp schlegelp left a comment

Choose a reason for hiding this comment

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

Thanks! I'm a bit worried about always changing the print option. We really only need this when running the tests. I have some local changes where this is under the control of an environment variable:

if os.environ.get('NAVIS_TEST_ENV', '').lower() == 'true':
    import numpy as np
    np.set_printoptions(legacy="1.25")

This would then require also changes to test_package.yml and pytest.ini to set that environment variable. Alternatively, you could invoke the set_printoptions in conftest.py - I haven't tried that though.

# Set up numpy number representation, see NEP51
# Once numpy<=2 is dropped from requirements, the doctest comparissons
# should become `np.float64(1.074)` instead of `1.074`
if int(np.version.version.split('.')[0])>=2:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would put this in config.py - feels more appropriate when adjusting global settings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the suggested changes.

In this scenario, the whole code basis gets switched to a different representation of np values. This means, in theory there could a piece of code that behaves differently in the NAVIS_TEST_ENV than it does whenever you run the code without that variable set to true. I can't think of a case where this would matter. Since you were worried about "global" changes to the representation / print option, I also made an alternative PR #177, where the change of number representation should only change within the pydoctest.

Either #176 or #177 should be accepted, not both.

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