This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients ncoghlan
Date 2017-12-06.04:33:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1512534786.28.0.213398074469.issue32229@psf.upfronthosting.co.za>
In-reply-to
Content
One of the observations coming out of the PEP 565 discussions is that it's surprisingly tricky to opt-in to getting all warnings from a particular package and its subpackages, while opting out of warnings in general.

The simplest approximation is to do the following:

    if not sys.warnoptions:
        warnings.simplefilter("ignore")
        warnings.filterwarnings("default", module="app_pkg.*")

That shows warnings for any module or package starting with `app_pkg`. A stricter filter that avoided warnings from top-level packages that merely shared the prefix would look like:

    if not sys.warnoptions:
        warnings.simplefilter("ignore")
        warnings.filterwarnings("default", module="^app_pkg(\..*)?$")

It could be helpful to encapsulate that logic in a more declarative utility API, such that applications could do the following:

    import warnings.
    warnings.hide_warnings()

Or:

    import warnings.
    warnings.hide_warnings(override_warnoptions=True)

Or:

    import warnings.
    warnings.hide_warnings(show=["app_pkg"])

Proposed API:

    def hide_warnings(*, show=(), override_warnoptions=False):
        if override_warnoptions or not sys.warnoptions:
            simplefilter("ignore")
            for pkg_name in show:
                pkg_filter =  _make_regex_for_pkg(pkg_name)
                filterwarnings("default", module=pkg_filter)
History
Date User Action Args
2017-12-06 04:33:06ncoghlansetrecipients: + ncoghlan
2017-12-06 04:33:06ncoghlansetmessageid: <1512534786.28.0.213398074469.issue32229@psf.upfronthosting.co.za>
2017-12-06 04:33:06ncoghlanlinkissue32229 messages
2017-12-06 04:33:04ncoghlancreate