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

Ignore deprecation warnings about old-style __init__/__setstate__ constructors in the tests (originally done in #2746) #2759

Merged
merged 3 commits into from
Jan 1, 2021

Conversation

YannickJadoul
Copy link
Collaborator

@YannickJadoul YannickJadoul commented Dec 30, 2020

Description

As the title says. The test suite contains a few tests to make sure the old-style __init__ (with placement news) still works and interacts correctly with the new, non-deprecated way of doing things. Using a debug build of Python (in #2746), pybind11 shows these deprecation warnings, so let's filter these out to remove some output clutter and make sure we have a higher chance of catching useful warnings.

Suggested changelog entry:

Suppressed some deprecation warnings about old-style ``__init__``/``__setstate__`` in the tests.


template <typename F>
void ignoreOldStyleInitWarnings(F &&body) {
py::exec(R"(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment from #2746:

  • @YannickJadoul: For reference, this was the old code, without py::exec (and thus without requiring <pybind11/eval.h> to be included in pybind11_tests.h and thus in all tests).

    @bstaletic slightly prefers this [new version], and I think I do to. However, curious to learn what other reviewers think.

    /// Simplified ``with warnigns.catch_warnings()`` wrapper
    template <typename F>
    void ignoreOldStyleInitWarnings(F &&body) {
        auto message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated";
        auto category = py::reinterpret_borrow<py::object>(PyExc_FutureWarning);
        auto warnings = py::module_::import("warnings");
        auto context_mgr = warnings.attr("catch_warnings")();
        context_mgr.attr("__enter__")();
        warnings.attr("filterwarnings")("ignore", py::arg("message")=message, py::arg("category")=category);
        body();
        // Exceptions in `body` not handled; see PEP 343 when these would need to be added
        context_mgr.attr("__exit__")(py::none(), py::none(), py::none());
    }

Copy link
Collaborator

@henryiii henryiii left a comment

Choose a reason for hiding this comment

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

All test changes to avoid warnings because we are intentionally checking things that warn, looks good to me.

@YannickJadoul YannickJadoul added this to the v2.6.2 milestone Jan 1, 2021
Copy link
Collaborator

@bstaletic bstaletic left a comment

Choose a reason for hiding this comment

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

Since I helped with the PR, am I allowed to approve?

@YannickJadoul
Copy link
Collaborator Author

Let's merge this, then? If anyone objects against #include <pybind11/eval.h> in pybind11_tests.h, we can still change it!

@YannickJadoul YannickJadoul merged commit 98f1bbb into pybind:master Jan 1, 2021
@YannickJadoul YannickJadoul deleted the old-style-init-warnings branch January 1, 2021 16:37
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jan 1, 2021
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Jan 25, 2021
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