Skip to content

Enhanced format inference in unified I/O interface#19960

Open
Iquecs2005 wants to merge 4 commits into
astropy:mainfrom
Iquecs2005:Enhanced_Format_Inference
Open

Enhanced format inference in unified I/O interface#19960
Iquecs2005 wants to merge 4 commits into
astropy:mainfrom
Iquecs2005:Enhanced_Format_Inference

Conversation

@Iquecs2005

Copy link
Copy Markdown

Description

This pull request is to address the improvement of the unified I/O interface format inference, allowing for identifier functions to raise errors for better error clarity, grouped in an ExceptionGroup IOIdentifierExceptions, and that doesn't trigger when a valid format is found.

Fixes #18398

  • 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.

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

I think something like this is a good idea! I'm on vacation this week so hopefully another maintainer can review.

@pllim pllim added this to the v8.1.0 milestone Jun 23, 2026
@pllim pllim requested a review from taldcroft June 23, 2026 15:18
@taldcroft

Copy link
Copy Markdown
Member

@Iquecs2005 - thanks for the contribution!

With a bit of time passing, I'm not so keen on my original idea to swallow exceptions. This could hide real bugs and in some cases cause even more confusion. But I think a middle ground that builds on your PR is possible.

Instead of swallowing exceptions, just raise on the first one with an exception message that should help most users. I wanted to play around with this and came up with the following. The work you've put into testing should still be mostly applicable.

        valid_formats = []
        for data_format, data_class in self._identifiers:
            if self._is_best_match(data_class_required, data_class, self._identifiers):
                identify_func = self._identifiers[(data_format, data_class)]
                try:
                    if identify_func(origin, path, fileobj, *args, **kwargs):
                        valid_formats.append(data_format)
                except Exception as e:
                    raise RuntimeError(
                        f"Unexpected exception identifying format {data_format!r} "
                        f"in function {identify_func.__name__!r}.\n\n"
                        "Try specifying the file format using the 'format' argument to avoid this error."
                    ) from e

        return valid_formats

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.

Improve format inference in the unified I/O interface

4 participants