Message307701
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) |
|
Date |
User |
Action |
Args |
2017-12-06 04:33:06 | ncoghlan | set | recipients:
+ ncoghlan |
2017-12-06 04:33:06 | ncoghlan | set | messageid: <1512534786.28.0.213398074469.issue32229@psf.upfronthosting.co.za> |
2017-12-06 04:33:06 | ncoghlan | link | issue32229 messages |
2017-12-06 04:33:04 | ncoghlan | create | |
|