From c1d239103c0060212e59f99171ccc4d8405420eb Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sun, 6 Nov 2022 17:24:52 -0600 Subject: Fix stacklevel when warning invalid config setting; added assertWarns wrapper similar to assertRaises wrapper, to echo success/fail status --- pyparsing/util.py | 3 ++- tests/test_unit.py | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pyparsing/util.py b/pyparsing/util.py index b3db48a..2a06e39 100644 --- a/pyparsing/util.py +++ b/pyparsing/util.py @@ -23,7 +23,8 @@ class __config_flags: if dname in cls._fixed_names: warnings.warn( f"{cls.__name__}.{dname} {cls._type_desc} is {str(getattr(cls, dname)).upper()}" - f" and cannot be overridden" + f" and cannot be overridden", + stacklevel=3, ) return if dname in cls._all_names: diff --git a/tests/test_unit.py b/tests/test_unit.py index cdb9691..975b65b 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -101,12 +101,28 @@ class TestCase(unittest.TestCase): if getattr(ar, "exception", None) is not None: print( - f"Raised expected exception: {type(ar.exception).__name__}: {str(ar.exception)}" + f"Raised expected exception: {type(ar.exception).__name__}: {ar.exception}" ) else: print(f"Expected {expected_exception_type.__name__} exception not raised") return ar + @contextlib.contextmanager + def assertWarns(self, expected_warning_type: Any, msg: Any = None): + """ + Simple wrapper to print out the warnings raised after assertWarns + """ + with super().assertWarns(expected_warning_type, msg=msg) as ar: + yield + + if getattr(ar, "warning", None) is not None: + print( + f"Raised expected warning: {type(ar.warning).__name__}: {ar.warning}" + ) + else: + print(f"Expected {expected_warning_type.__name__} warning not raised") + return ar + @contextlib.contextmanager def assertDoesNotWarn(self, warning_type: type = UserWarning, msg: str = None): with warnings.catch_warnings(record=True) as w: -- cgit v1.2.1