Skip to content

BUG: avoid assert statements in business logic (io.ascii) - #17477

Merged
mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:io.ascii/bug/avoid_assert_in_business_logic
Nov 29, 2024
Merged

mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:io.ascii/bug/avoid_assert_in_business_logic

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

ref #17472

  • 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 io.ascii/bug/avoid_assert_in_business_logic branch from 4fec8ca to 4e6db10 Compare November 29, 2024 10:26
@neutrinoceros
neutrinoceros marked this pull request as ready for review November 29, 2024 10:45
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Note that there are still 4 failing tests with

PYTHONOPTIMIZE=2 pytest astropy/io/ascii

but they all have the same, orthogonal failure mode:
astropy.io.registry.interface.UnifiedReadWrite.help relies on accessing __doc__ attributes at runtime

def help(self, format=None, out=None):

I don't see an easy way out other than skipping these tests under optimized mode; the alternative being to refactor every method whose __doc__ attribute can be hit there and move the docstring to another attribute (the docstring would still be aliased to it, of course); this would be a substantial effort (probably not worth the expected gain), and wouldn't even work for external subclasses.

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

Good to get rid of the functional asserts, but may as well aim to make the code clearer...

Comment thread astropy/io/ascii/core.py Outdated
@@ -1075,7 +1075,8 @@ def _validate_and_copy(col, converters):
# The input `converters` must be a ``type`` value that can init np.dtype.
try:

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.

Can we just do

if not isinstance(converters, type):
    try:
        converters = [np.dtype(converters)]
    except TypeError:
        pass

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.

Taking this suggestion directly doesn't work (2 tests failures), but you're right that this logic can be simplified. I'll push what I think is the correct version of what you're proposing

Comment thread astropy/io/ascii/ui.py Outdated
try:
new_val = cls(val)
assert new_val == val
if not new_val == val:

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.

Here, I'd replace the assert with ok = new_val == val in the else clause.

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.

much better, thanks !

@neutrinoceros
neutrinoceros force-pushed the io.ascii/bug/avoid_assert_in_business_logic branch from 4e6db10 to e70f979 Compare November 29, 2024 17:42

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

Ah, I guess the np.dtype was meant for things like float, np.int64 etc.

Looks good to me now - this obviously does the same as what was there before, so let's get it in.

@mhvk
mhvk merged commit b536398 into astropy:main Nov 29, 2024
@neutrinoceros
neutrinoceros deleted the io.ascii/bug/avoid_assert_in_business_logic branch November 29, 2024 18:11
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.

2 participants