Skip to content
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

Redirect warning output from stderr to stdout in doctests #39647

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

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 7a1c8e9; 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.

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