Skip to content

Fix spurious invalid-value warning from Angle.to_string on some Python builds - #20618

Open
jdavies-st wants to merge 2 commits into
astropy:mainfrom
jdavies-st:angle-to-string-vectorize-spurious-warnings
Open

jdavies-st wants to merge 2 commits into
astropy:mainfrom
jdavies-st:angle-to-string-vectorize-spurious-warnings

Conversation

@jdavies-st

@jdavies-st jdavies-st commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

On some Python builds, Angle.to_string(), and so SkyCoord.to_string(), gives a warning even though nothing is wrong:

RuntimeWarning: invalid value encountered in do_format (vectorized)

This pull request removes that warning.

What changes for users

  • No more spurious warning. This matters most for test suites that turn warnings into errors, where it was an error. pyvo's tests, for example, fail with it if you run them with the uv-installed CPython. They don't filter for it.
  • The strings returned are exactly the same as before.
  • Masked angles still give masked strings with the same mask.

Why it happened

to_string formats each value with np.vectorize. After each call, numpy checks whether the processor recorded an "invalid value" during the calculation and, if so, warns. On the x86_64 Linux Python builds that uv installs, formatting a number can leave that record behind even though the result is fine, so numpy warns about a problem that does not exist. More detail is in #18989, astral-sh/python-build-standalone#934 and numpy/numpy#27760.

The change in this PR

  • to_string now tells numpy to ignore "invalid value" reports while it formats the values. Formatting never produces a real invalid value: NaN is handled before formatting, and on a build without this problem, main gives no warning for special values (NaN, +-inf, very large and very small numbers, +-0) and formatting options.
  • The filter in pyproject.toml that supressed this warning in astropy's tests is removed. astropy's Linux CI uses the affected astral/uv Python builds, so the existing to_string tests now fail if the warning comes back.
  • A new test checks that a masked Angle keeps its mask.

#19021 took a different approach and replaced np.vectorize with a loop. That also stopped the warning, but it had the unfortunate side effect of turning masked angles arrays into plain string arrays, formatting the masked values as if they were valid. Keeping np.vectorize avoids that, and the new test checks it.

Fixes #18989

AI Disclosure

Claude Opus 5.5 was used to help investigate, diagnose and draft the bugfix.

  • I certify that I am human and that I take full responsibility for this pull request including all interactions with reviewers.

Merge method

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • 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 instructions for rebase and squash.
  • 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. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • 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.

@jdavies-st
jdavies-st force-pushed the angle-to-string-vectorize-spurious-warnings branch from 92ab7c1 to 804add9 Compare October 15, 2026 15:13
@pllim pllim added this to the v7.2.3 milestone Oct 15, 2026
@pllim pllim added Bug backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x labels Oct 15, 2026
Comment thread astropy/coordinates/angles/core.py Resolved
@pllim
pllim requested a review from mhvk October 15, 2026 16:04
@astrofrog astrofrog modified the milestones: v7.2.3, v7.2.4 Oct 16, 2026
@jdavies-st
jdavies-st force-pushed the angle-to-string-vectorize-spurious-warnings branch 2 times, most recently from 5e4f36c to a6b2b0f Compare October 17, 2026 10:16
- Wrap the np.vectorize call in np.errstate(invalid="ignore"). A function
  run through np.vectorize warns about floating-point flags that Python
  code leaves set, which uv's x86_64 Python builds do while formatting
- Remove the pyproject.toml filter that ignored the warning
- Add a test that a masked Angle keeps its mask
@jdavies-st
jdavies-st force-pushed the angle-to-string-vectorize-spurious-warnings branch from a6b2b0f to fa2f577 Compare October 27, 2026 17:54
@jdavies-st

Copy link
Copy Markdown
Contributor Author

@mhvk would be happy for a review on this, given you tried fixing this as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x Bug coordinates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runtime error from Angle.to_string() on older python/numpy combinations

4 participants