Skip to content

Handle missing uncompresspy in fitsinfo without traceback#19869

Open
mokashang wants to merge 2 commits into
astropy:mainfrom
mokashang:fix/fitsinfo-fitsdiff-uncompresspy
Open

Handle missing uncompresspy in fitsinfo without traceback#19869
mokashang wants to merge 2 commits into
astropy:mainfrom
mokashang:fix/fitsinfo-fitsdiff-uncompresspy

Conversation

@mokashang
Copy link
Copy Markdown

Fixes #19852.

Problem

Running fitsinfo on an LZW-compressed .Z file currently crashes with an uncaught ModuleNotFoundError traceback when the optional uncompresspy dependency is not installed:

$ fitsinfo astropy/io/fits/tests/data/lzw.fits.Z
Traceback (most recent call last):
  File ".../fitsinfo", line 6, in <module>
    sys.exit(main())
  ...
  File ".../astropy/io/fits/file.py", line 614, in _try_read_compressed
    raise ModuleNotFoundError(
ModuleNotFoundError: The optional package uncompresspy is necessary for reading LZW compressed files (.Z extension).

fitsinfo.fitsinfo() only catches OSError, so the ModuleNotFoundError raised by _BaseHDU._readfrom_File.__init___try_read_compressed propagates up uncaught.

Fix

Broaden the except in fitsinfo() to also catch ImportError and log a clean error message, matching the behaviour of fitsdiff (which already handles this case — FITSDiff.__init__ wraps any exception from fitsopen in OSError, so its existing except OSError happens to catch this).

After the fix:

$ fitsinfo astropy/io/fits/tests/data/lzw.fits.Z
ERROR: The optional package uncompresspy is necessary for reading LZW compressed files (.Z extension). [astropy.io.fits.scripts.fitsinfo]

Scope

This PR only addresses fitsinfo. The issue mentions "fitsinfo (and probably fitsdiff too)", but I verified fitsdiff already produces a clean Warning: failed to open ... message on the same input (covered by the existing test_warning_unreadable_file test in test_fitsdiff.py, added in #18882).

Testing

  • Added test_missing_optional_dependency in astropy/io/fits/tests/test_fitsinfo.py, modelled on the existing test_warning_unreadable_file for fitsdiff. It monkeypatches HAS_UNCOMPRESSPY to False and asserts that running fitsinfo on lzw.fits.Z logs an error mentioning uncompresspy rather than raising.
  • Verified the new test fails on main (with the original ModuleNotFoundError traceback) and passes with this fix.
  • Full astropy/io/fits/tests/ suite passes locally (1078 passed, 29 skipped — the skipped tests are the ones that require uncompresspy to actually be installed).

Changelog

Added docs/changes/io.fits/19852.bugfix.rst. Happy to rename it to the PR number once assigned.

When the optional dependency uncompresspy is not installed, running
fitsinfo on an LZW-compressed .Z file currently raises an
uncaught ModuleNotFoundError and prints a long traceback.  Catch
ImportError alongside the existing OSError so the user instead
sees a clean error message, matching the behaviour of fitsdiff
(which already catches this case via FITSDiff.__init__).

Includes a regression test that mirrors the existing fitsdiff test by
monkeypatching HAS_UNCOMPRESSPY to False and running the script
against the bundled lzw.fits.Z data file.

Fixes astropy#19852
@mokashang mokashang requested a review from saimn as a code owner June 5, 2026 16:21
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 5, 2026

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.

@pllim pllim added this to the v8.1.0 milestone Jun 5, 2026
@pllim pllim added the Bug label Jun 5, 2026
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.

BUG: fitsinfo/fitsdiff crashes when uncompresspy is not installed

2 participants