Skip to content

DEPR: deprecate astropy.utils.check_broadcast - #16346

Merged
mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:depr/deprecate_check_broadcast
Apr 29, 2024
Merged

mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:depr/deprecate_check_broadcast

Conversation

@neutrinoceros

@neutrinoceros neutrinoceros commented Apr 26, 2024 •

Copy link
Copy Markdown
Contributor

Description

Fixes #15211 and replaces #15499 (I marked @felipekhouri as a co-author here)

I note that this implies IncompatibleShapeError is now unused (outside check_broadcast. Since users might be importing it to construct try/except blocks around check_broadcast, it should probably be deprecated too. Let me know if this should be done in the same PR or as a follow up.

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

@github-actions

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch from a99383f to 8692aa8 Compare April 26, 2024 06:24
Comment thread astropy/modeling/core.py Resolved
Comment thread astropy/modeling/core.py Resolved
Comment thread astropy/modeling/core.py Outdated
r"arg (?P<argno_b>\d+) with shape (?P<shape_b>\((\d+(, ?)?)+\))\."
)
if (match := broadcast_shapes_error_re.fullmatch(str(exc))) is None:
warnings.warn(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since parsing the error message emitted by numpy is intrinsically fragile (but probably our least-bad option), I spent extra effort on making this logic as robust as possible: failing to parse an error message should be recoverable.

@neutrinoceros
neutrinoceros marked this pull request as ready for review April 26, 2024 07:45
@neutrinoceros
neutrinoceros requested a review from a team as a code owner April 26, 2024 07:45
@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch from 9c86bcc to ef1d30e Compare April 26, 2024 07:57
Comment thread astropy/modeling/core.py Resolved
@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch 2 times, most recently from 4f0a0a5 to 45aac14 Compare April 26, 2024 11:16
Comment thread astropy/utils/exceptions.py Outdated
Comment on lines +78 to +87
def _add_note_to_exception(exc: Exception, note: str) -> Exception:
import sys

if sys.version_info >= (3, 11):
exc.add_note(note)
return exc
else:
# mimic Python 3.11 behavior:
# preserve error message and traceback
return type(exc)(f"{exc!s}\n{note}").with_traceback(exc.__traceback__)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a side improvement but isn't strictly needed here, I just applied an existing snippet of mine here. Let me know if I should revert it or move it to a separate PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I sligtly like preserving the same instance. In other code, I've done the equivalent of exc.args += ("\n" + note,). Would that work here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hum, I didn't think about that. I guess replacing a tuple attribute is objectively less evil than returning an entirely new instance in a context where we accept mutations anyway. Thanks !

@neutrinoceros neutrinoceros changed the title DEPR: deprecate astropy.utils.check_broadcast DEPR: deprecate astropy.utils.check_broadcast Apr 26, 2024
@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch 2 times, most recently from 83e4540 to fd5c4c0 Compare April 26, 2024 12:48
@pllim pllim added this to the v7.0.0 milestone Apr 26, 2024
@pllim pllim added the API change PRs and issues that change an existing API, possibly requiring a deprecation period label Apr 26, 2024
@pllim
pllim requested review from lpsinger and mhvk April 26, 2024 18:23

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks mostly excellent. But I really would prefer no code changes/comments just for test behaviour - hence the query whether that could be dealt with in the tests themselves. The rest is mostly small stuff.

Comment thread astropy/modeling/core.py Outdated
try:
broadcast_shape = check_broadcast(*broadcasted_shapes[0])
except (IndexError, TypeError):
# always promote deprecation warnings to exceptions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this really necessary? I feel the real code should not have to worry about this. Is it not possible to adjust a test?

p.s. Wish I could trust that except Exception was not covered by the tests...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this really necessary?

For reference, the one test that failed before I applied this change was modeling/tests/test_core.py::test_inputless_model; It failed only in the context of wheel testing where the relevant difference with regular CI is that warnings are not elevated to errors.
See example logs
I haven't found a way to adjust the test itself yet, but I agree that this patch leaves a bad taste. I'll circle back to this.

p.s. Wish I could trust that except Exception was not covered by the tests...

From my experience running this is CI, it is covered, or I wouldn't have needed the aforementioned patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

note to self: I think Model._prepare_inputs_single_model underwent an implicit change of behavior and needs adjusting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just pushed what I think is a more maintainable solution, together with a companion comment explaining my findings.

Comment thread astropy/modeling/core.py Resolved
Comment thread astropy/modeling/tests/test_input.py Outdated
r" 'p1' of shape .*"
)
with pytest.raises(ValueError, match=MESSAGE):
with pytest.raises(ValueError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I never liked full matching of error messages, but ValueError is very broad, so maybe keep a simple match="broadcast"? (here and elsewhere in file)

Comment thread astropy/modeling/tests/test_models.py Outdated
with pytest.raises(
ValueError, match="All inputs must have identical shapes or must be scalars"
):
with pytest.raises(ValueError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe again a match="broadcast"

Comment thread astropy/utils/exceptions.py Outdated
Comment on lines +78 to +87
def _add_note_to_exception(exc: Exception, note: str) -> Exception:
import sys

if sys.version_info >= (3, 11):
exc.add_note(note)
return exc
else:
# mimic Python 3.11 behavior:
# preserve error message and traceback
return type(exc)(f"{exc!s}\n{note}").with_traceback(exc.__traceback__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I sligtly like preserving the same instance. In other code, I've done the equivalent of exc.args += ("\n" + note,). Would that work here?

Comment thread astropy/utils/tests/test_shapes.py Resolved
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@mhvk thanks for your review. I think I addressed all your minor comments. I'll circle back to the more important one on Monday !

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good!

Final question: did you try ignoring all warnings in test_check_broadcast? If that also doesn't work, then let's get things in as they are. Thanks!

@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch from ad37735 to 7ae188f Compare April 29, 2024 07:14
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Thanks for reminding me. I updated the test with your suggestion then squashed all commits together.

@neutrinoceros
neutrinoceros force-pushed the depr/deprecate_check_broadcast branch from 7ae188f to 796c9c2 Compare April 29, 2024 07:36

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Super, let's get it in!

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

Labels

API change PRs and issues that change an existing API, possibly requiring a deprecation period coordinates modeling utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate check_broadcast

3 participants