New submission from Nick Coghlan <[email protected]>:
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)
----------
messages: 307701
nosy: ncoghlan
priority: normal
severity: normal
stage: needs patch
status: open
title: Simplify hiding developer warnings in user facing applications
type: enhancement
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32229>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com