Skip to content

Redirect warning output from stderr to stdout in doctests #39647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025

Conversation

tobiasdiez
Copy link
Contributor

@tobiasdiez tobiasdiez commented Mar 7, 2025

Pytest considers ouput in stderr as errors, thus we redirect the doctest warnings to stdout instead. Doesn't seem to make a difference for sage's doctest runner.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

Copy link

github-actions bot commented Mar 7, 2025

Documentation preview for this PR (built with commit 5d573c3; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@user202729
Copy link
Contributor

Seems semantically incorrect because…

    INPUT: see :func:`warnings.showwarning`.

https://docs.python.org/3/library/warnings.html#warnings.showwarning

warnings.showwarning(message, category, filename, lineno, file=None, line=None)

    Write a warning to a file. The default implementation calls formatwarning(message, category, filename, lineno, line) and writes the resulting string to file, which defaults to [sys.stderr](https://docs.python.org/3/library/sys.html#sys.stderr).

How would programs that are tested using pytest normally test for warnings to be raised then?

@tobiasdiez
Copy link
Contributor Author

Seems semantically incorrect because…

    INPUT: see :func:`warnings.showwarning`.

https://docs.python.org/3/library/warnings.html#warnings.showwarning

warnings.showwarning(message, category, filename, lineno, file=None, line=None)

    Write a warning to a file. The default implementation calls formatwarning(message, category, filename, lineno, line) and writes the resulting string to file, which defaults to [sys.stderr](https://docs.python.org/3/library/sys.html#sys.stderr).

Right, but in the context of doctest the distinction between stdout and stderr doesn't seem to be relevant (both are printed-out as strings by default).

How would programs that are tested using pytest normally test for warnings to be raised then?

In normal pytest methods, one can use the capsys fixture. But this doesn't work for doctests.

Sympy has a [warns context manager](https://docs.sympy.org/latest/modules/testing/pytest.html#sympy.testing.pytest.warns] that checks that a given warning is emitted. I think having something like this in place also for doctests would be helpful and would make the tests more robust. But this is something for another PR.

@user202729
Copy link
Contributor

in the context of doctest

If this function is to be used nowhere outside doctest then I suppose this is acceptable. But I suggest modify the documentation for file= in the docstring then.

Is there really no other occurrence of stderr being used? This feels weird.

@tobiasdiez
Copy link
Contributor Author

in the context of doctest

If this function is to be used nowhere outside doctest then I suppose this is acceptable.

Yes, it's part of doctest.forker and not used anywhere else.

But I suggest modify the documentation for file= in the docstring then.

Done.

Is there really no other occurrence of stderr being used? This feels weird.

Not that I could find.

@tobiasdiez
Copy link
Contributor Author

@user202729 does this look good to you, or do you have further suggestions?

vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 1, 2025
sagemathgh-39647: Redirect warning output from stderr to stdout in doctests
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

Pytest considers ouput in `stderr` as errors, thus we redirect the
doctest warnings to `stdout` instead. Doesn't seem to make a difference
for sage's doctest runner.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [ ] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39647
Reported by: Tobias Diez
Reviewer(s):
@vbraun vbraun merged commit b40d3f9 into sagemath:develop Apr 2, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants