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 warnings with Python 3.10 #11962

Merged
merged 12 commits into from
Sep 9, 2021
Merged

Fix warnings with Python 3.10 #11962

merged 12 commits into from
Sep 9, 2021

Conversation

saimn
Copy link
Contributor

@saimn saimn commented Jul 20, 2021

Closes #11821, tests run locally, not sure when we will be able to add a job on Actions.

Fix the deprecation warnings and ignore new RuntimeWarning raised in convolution, lombscargle, modeling and units. We should probably address those by either fixing the code or catching them in tests but there are many of them (except for units, only 1 IIRC). So probably to address those when we have a 3.10 job.

What should be the milestone, probably 4.3 ?

Description

This pull request is to address ...

Fixes #

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the Extra CI label.
  • Is a change log needed? If yes, did the change log check pass? If no, add the no-changelog-entry-needed label.
  • Is a milestone set? Milestone must be set but astropy-bot check might be missing; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate backport-X.Y.x label(s) before merge.

with warnings.catch_warnings():
# distutils.sysconfig module is deprecated in Python 3.10
warnings.simplefilter('ignore', DeprecationWarning)
_convolve = load_library("_convolve", LIBRARY_PATH)
Copy link
Member

Choose a reason for hiding this comment

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

This fix does not look like it is going to work when the deprecated feature is removed. Is there no replacement for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Numpy has some plan : numpy/numpy#18588
But here we are using a smaller helper function that relies on distutils to get the shared library extension on different platforms. We could replace this with some custom code I guess.

Copy link
Member

Choose a reason for hiding this comment

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

So, instead of merging this, which would completely mask the real problem, should we wait a bit and see? Not like we can test against Python 3.10 in CI right now anyway...

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 don't think they will fix this in a few weeks, and we/they have time until 3.12
Actions seems to support 3.10b4 now so I'm trying to add a job.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, as long as we open follow-up issue about this, I guess I can live with this for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, hopefully this can be an "upstream fix required"!

with warnings.catch_warnings():
# distutils package is deprecated in Python 3.10
warnings.simplefilter('ignore', DeprecationWarning)
from distutils.version import LooseVersion
Copy link
Member

Choose a reason for hiding this comment

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

Oh no... How do we replace this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You should know : #11714 ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it seems time to get back to that one!!

setup.cfg Outdated
Comment on lines 147 to 150
# RuntimeWarning from convolution, lombscargle, modeling
ignore:invalid value encountered in true_divide:RuntimeWarning
# RuntimeWarning from units, lombscargle
ignore:divide by zero encountered in:RuntimeWarning
Copy link
Member

Choose a reason for hiding this comment

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

I am not comfortable with blanket ignore like this. They should be ignored on a case-by-case basis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but it's more work, and at least for lombscargle I have no idea how to fix it.

Copy link
Member

Choose a reason for hiding this comment

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

I still have a bad feeling about this. This will ignore such warnings for the entire repo, not just the affected subpackages.

@saimn
Copy link
Contributor Author

saimn commented Jul 20, 2021

Just as a remainder, Python 3.10's release schedule:

    3.10.0 beta 4: Saturday, 2021-07-10

Expected:

    3.10.0 candidate 1: Monday, 2021-08-02
    3.10.0 candidate 2: Monday, 2021-09-06 (if necessary)
    3.10.0 final: Monday, 2021-10-04

@saimn
Copy link
Contributor Author

saimn commented Jul 20, 2021

For the record, convolution has 573 failed tests, I think all because of the same warning:

>               normalized_kernel = kernel / kernel_scale
E               RuntimeWarning: invalid value encountered in true_divide

astropy/convolution/convolve.py:691: RuntimeWarning

where kernel_scale is (1+0j).

modeling has only one failing test, also because of a division by 0j.

lombscargle fails on something like numerator / denominator where numerator is complex and denominator is real, so again division by 0j but not the same warning ...

@saimn
Copy link
Contributor Author

saimn commented Jul 20, 2021

And the 3.10 job passes 🎉

@pllim
Copy link
Member

pllim commented Jul 20, 2021

Re: #11962 (comment) -- Can we localize the warning ignore to the places you have identified instead?

@pllim pllim mentioned this pull request Jul 20, 2021
2 tasks
setup.cfg Outdated
ignore:divide by zero encountered in true_divide:RuntimeWarning:astropy.timeseries.periodograms.lombscargle
ignore:divide by zero encountered in true_divide:RuntimeWarning:astropy.units.quantity
ignore:invalid value encountered in true_divide:RuntimeWarning:astropy.convolution.convolve
ignore:invalid value encountered in true_divide:RuntimeWarning:astropy.modeling.functional_models
Copy link
Member

@pllim pllim Jul 20, 2021

Choose a reason for hiding this comment

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

Okay, I guess this is a good compromise. 😸 (And need a follow-up issue.)

@@ -58,6 +58,11 @@ jobs:
python: 3.9
toxenv: py39-test

- name: Python 3.10 with minimal dependencies
Copy link
Member

Choose a reason for hiding this comment

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

Originally when I attempted this, I put this in devdeps job.

I also wonder if "minimal dependencies" is enough to catch everything...

Copy link
Member

Choose a reason for hiding this comment

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

Also would need a follow up issue to test with proper Python 3.10 when it is released for real.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we'll need to update for rc1 and the final release.
I didn't try with all other dependencies because I'm afraid this will fail somewhere. But it's fine for now, I mean the goal here is to test that we are compatible with 3.10, not to test that other dependencies also are.

Copy link
Member

Choose a reason for hiding this comment

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

Long term I think it's worth bundling this with dev dependencies, especially if they provide a nightly build with dev/rc python.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Not bad, actually! Overall, looks good. I'm surprised by the warning on the one quantity test, though...

@@ -473,7 +473,7 @@ def set_enabled_equivalencies(equivalencies):
>>> from astropy import units as u
>>> with u.set_enabled_equivalencies(u.dimensionless_angles()):
... phase = 0.5 * u.cycle
... np.exp(1j*phase) # doctest: +FLOAT_CMP
... np.exp(1j*phase) # doctest: +FLOAT_CMP +IGNORE_WARNINGS
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I'd be interested to know what the actual warning was. I cannot find this in #11821. Is there perhaps a log I can look at? (I've yet to install a beta version of python; if that's easy, I'll do that!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same problem as all others failures, dividing a complex by a scalar now raises a warning because of the division by 0j:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
465         list of equivalent pairs, e.g., as returned by
466         `~astropy.units.equivalencies.dimensionless_angles`.
467 
468     Examples
469     --------
470     Exponentiation normally requires dimensionless quantities.  To avoid
471     problems with complex phases::
472 
473         >>> from astropy import units as u
474         >>> with u.set_enabled_equivalencies(u.dimensionless_angles()):
UNEXPECTED EXCEPTION: RuntimeWarning('divide by zero encountered in cdouble_scalars')
Traceback (most recent call last):
  File "/home/sconseil/.pyenv/versions/3.10.0b4/lib/python3.10/doctest.py", line 1348, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest astropy.units.core.set_enabled_equivalencies[1]>", line 3, in <module>
  File "/home/sconseil/dev/astropy/astropy/astropy/units/quantity.py", line 504, in __array_ufunc__
    arrays.append(converter(input_) if converter else input_)
  File "/home/sconseil/dev/astropy/astropy/astropy/units/core.py", line 988, in convert
    return func(_condition_arg(v) / scale1) * scale2
RuntimeWarning: divide by zero encountered in cdouble_scalars
/home/sconseil/dev/astropy/astropy/astropy/units/core.py:474: UnexpectedException
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /home/sconseil/dev/astropy/astropy/astropy/units/core.py(988)convert()
-> return func(_condition_arg(v) / scale1) * scale2
(Pdb) _condition_arg(v)
0.5j
(Pdb) scale1
0.15915494309189535
(Pdb) _condition_arg(v) / scale1
*** RuntimeWarning: divide by zero encountered in cdouble_scalars

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this python 3.10 specific? This is completely unphysical, so I'd want to raise a bug report...

with warnings.catch_warnings():
# distutils package is deprecated in Python 3.10
warnings.simplefilter('ignore', DeprecationWarning)
from distutils.version import LooseVersion
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it seems time to get back to that one!!

with warnings.catch_warnings():
# distutils.sysconfig module is deprecated in Python 3.10
warnings.simplefilter('ignore', DeprecationWarning)
_convolve = load_library("_convolve", LIBRARY_PATH)
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, hopefully this can be an "upstream fix required"!

Copy link
Member

@pllim pllim left a comment

Choose a reason for hiding this comment

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

LGTM though we need to open several follow-up issues.

I won't merge yet in case subpackage maintainers want to review all the warnings in modeling (@nden , @perrygreenfield), timeseries (@astrofrog, @bsipocz), units (@adrn, @mhvk), and convolution (@larrybradley, @adonath, @keflavich).

@@ -58,6 +58,11 @@ jobs:
python: 3.9
toxenv: py39-test

- name: Python 3.10 with minimal dependencies
Copy link
Member

Choose a reason for hiding this comment

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

Long term I think it's worth bundling this with dev dependencies, especially if they provide a nightly build with dev/rc python.

@saimn
Copy link
Contributor Author

saimn commented Jul 20, 2021

To sum up, all warnings are caused by some kind of division of a complex by a scalar:

>>> np.complex128(1j) / 2
<stdin>:1: RuntimeWarning: divide by zero encountered in cdouble_scalars
0.5j

>>> np.array([1j]) / np.array([2])
<stdin>:1: RuntimeWarning: divide by zero encountered in true_divide
array([0.+0.5j])

>>> np.array([1 + 0j]) / 1+0j
<stdin>:1: RuntimeWarning: invalid value encountered in true_divide
array([1.+0.j])

@mhvk
Copy link
Contributor

mhvk commented Jul 20, 2021

Weird, so it is a numpy problem, but one that only comes up in python 3.10? I just installed python 3.10 via debian experimental, and indeed cannot reproduce that error with just 0.5j/2., so it is not directly a python problem. Unfortunately, I cannot seem to use this installation with venv or virtualenv, so it is not much use for further testing... But I did check that I do not get your warnings on numpy-dev/python 3.9.

One question: perhaps we should try with numpy-dev here as well? They seem to have made quite a few fixes for python 3.10 (though nothing that looks really relevant...). If this also gives problems with numpy-dev, they really should know about this...

@saimn
Copy link
Contributor Author

saimn commented Jul 21, 2021

Yes, it only happens on Python 3.10 and with Numpy complex values, not with Python ones. Maybe we can try with Numpy's nightly.

@pllim
Copy link
Member

pllim commented Jul 21, 2021

ERROR: Could not find a version that satisfies the requirement numpy (from versions: none)

Looks like Numpy dev not compatible with Python 3.10? 🤷

@saimn
Copy link
Contributor Author

saimn commented Jul 21, 2021

No, probably they do not build the nightly wheels for 3.10 yet.

@pllim
Copy link
Member

pllim commented Jul 21, 2021

Hmm passed on Python 3.10 and numpy-dev, it seems. But that is with the ignore still in place, right?

@saimn
Copy link
Contributor Author

saimn commented Jul 21, 2021

I commented the filters in the previous commit, so yeah it seems to be OK with numpy dev !
Maybe I revert the two last commits, and we remove the filters when the next version of numpy is out ?

@pllim
Copy link
Member

pllim commented Jul 21, 2021

Or... we just stick Python 3.10 into devdeps job and be done with it?

@saimn
Copy link
Contributor Author

saimn commented Jul 21, 2021

Installing Numpy from the git repo makes the job longer, compiling Numpy. It's an option but we can also wait for Numpy to publish nightly wheels for 3.10.

@pllim
Copy link
Member

pllim commented Sep 9, 2021

@saimn , there is conflict now. FYI

@saimn
Copy link
Contributor Author

saimn commented Sep 9, 2021

All green now :)

Copy link
Member

@pllim pllim left a comment

Choose a reason for hiding this comment

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

All the cron stuff are broken for different and unrelated reasons, so no point trying to run them here. Let's get this in. Thanks!

@pllim pllim merged commit 23404ee into astropy:main Sep 9, 2021
@lumberbot-app
Copy link

lumberbot-app bot commented Sep 9, 2021

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
$ git checkout v4.3.x
$ git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
$ git cherry-pick -m1 23404eeb2caeea315cea89b6cb8b9b2fee4f13be
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
$ git commit -am 'Backport PR #11962: Fix warnings with Python 3.10'
  1. Push to a named branch :
git push YOURFORK v4.3.x:auto-backport-of-pr-11962-on-v4.3.x
  1. Create a PR against branch v4.3.x, I would have named this PR:

"Backport PR #11962 on branch v4.3.x (Fix warnings with Python 3.10)"

And apply the correct labels and milestones.

Congratulation you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove Still Needs Manual Backport label once the PR gets merged.

If these instruction are inaccurate, feel free to suggest an improvement.

@pllim
Copy link
Member

pllim commented Sep 9, 2021

ops... Do we need to backport?

@saimn
Copy link
Contributor Author

saimn commented Sep 9, 2021

Having a 4.3.x compatible with Python 3.10 would be nice (it will out in ~1 month). I will look at the manual backport later.

@vstinner
Copy link

vstinner commented Sep 9, 2021

Thanks for the fix @saimn!

saimn pushed a commit to saimn/astropy that referenced this pull request Sep 10, 2021
saimn pushed a commit to saimn/astropy that referenced this pull request Oct 14, 2021
pllim added a commit to pllim/astropy that referenced this pull request Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test faillures with python 3.10.0a6
8 participants