Skip to content

Ensure doctests do not have side effects#17747

Merged
neutrinoceros merged 4 commits into
astropy:mainfrom
mhvk:doctest-cleanup
Feb 10, 2025
Merged

Ensure doctests do not have side effects#17747
neutrinoceros merged 4 commits into
astropy:mainfrom
mhvk:doctest-cleanup

Conversation

@mhvk

@mhvk mhvk commented Feb 9, 2025

Copy link
Copy Markdown
Contributor

In #17732, @pllim found that a double run of both the regular tests and the doctests caused failures. While in principle we should not necessarily be as strict for the doctests that there are no side effects, I thought we might as well clean it up. So, I did, and also picked @pllim's first commit from #17743 so that it gets tested.

Note may not fix #17745, as I haven't tied loading all the dependencies. One step at a time, I guess! (cc @neutrinoceros)

closes #17743 (since this PR uses its first commit that also includes the positional arguments)

@github-actions

github-actions Bot commented Feb 9, 2025

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.

Comment thread docs/io/ascii/read.rst
>>> for format_name in ['custom_no_header', 'custom_commented_header', 'fixed_width_commented_header']:
... registry.unregister_reader(f"ascii.{format_name}", Table)
... registry.unregister_writer(f"ascii.{format_name}", Table)
... del FORMAT_CLASSES[format_name]

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.

@taldcroft - it is probably not intended that format classes stay around even if you unregister the reader and the writer. It may not matter too much in practice, but without removing it, I get a failure in

def test_io_ascii_write():
"""
Test that table with mixin column can be written by io.ascii for
every pure Python writer. No validation of the output is done,
this just confirms no exceptions.
"""
from astropy.io.ascii.connect import _get_connectors_table
t = QTable(MIXIN_COLS)
for fmt in _get_connectors_table():
if fmt["Write"] and ".fast_" not in fmt["Format"]:
out = StringIO()
t.write(out, format=fmt["Format"])

Note that this uses a private method, so a user might never hit this problem.

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.

The io.ascii FORMAT_CLASSES is formally independent of registration as a Table reader/writer since users may be calling io.ascii.read(...). In the current paradigm, unregistering should not imply removal from FORMAT_CLASSES.

In theory we could decide to make io.ascii only be providing connect functions for unified I/O, and not provide any public API. But that would be a lot of churn without really helping all that much.

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.

OK, makes sense. I wondered about exposing some of this (also the units registry list), but think in the end it is not super useful and certainly not urgent: in general, I think people are pretty used to restarting python if something really gets messy with registered classes, etc.

@mhvk

mhvk commented Feb 9, 2025

Copy link
Copy Markdown
Contributor Author

Double test passed!

Comment thread docs/io/ascii/read.rst
>>> for format_name in ['custom_no_header', 'custom_commented_header', 'fixed_width_commented_header']:
... registry.unregister_reader(f"ascii.{format_name}", Table)
... registry.unregister_writer(f"ascii.{format_name}", Table)
... del FORMAT_CLASSES[format_name]

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.

The io.ascii FORMAT_CLASSES is formally independent of registration as a Table reader/writer since users may be calling io.ascii.read(...). In the current paradigm, unregistering should not imply removal from FORMAT_CLASSES.

In theory we could decide to make io.ascii only be providing connect functions for unified I/O, and not provide any public API. But that would be a lot of churn without really helping all that much.

@mhvk mhvk added Affects-dev PRs and issues that do not impact an existing Astropy release and removed Affects-dev PRs and issues that do not impact an existing Astropy release labels Feb 9, 2025

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

Thank you ! I agree that it's worth doing, and I'm glad I learned about pytest-doctestplus's testcleanup because of it.
I can also confirm this does not fix #17745 (though that's to be expected), but it also doesn't make it worse, so let's get this in !

@pllim

pllim commented Feb 10, 2025

Copy link
Copy Markdown
Member

Thanks! I was gonna backport my other PR. Let's see...

@meeseeksdev backport to v7.0.x

@mhvk mhvk deleted the doctest-cleanup branch February 10, 2025 14:04
pllim added a commit that referenced this pull request Feb 10, 2025
…747-on-v7.0.x

Backport PR #17747 on branch v7.0.x (Ensure doctests do not have side effects)
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Feb 10, 2025
… (Ensure doctests do not have side effects)
pllim added a commit that referenced this pull request Feb 10, 2025
…751-on-main

Backport PR #17751 on branch main (Backport PR #17747 on branch v7.0.x (Ensure doctests do not have side effects))
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.

TST: test pollution in scipy-only tests

4 participants