def changed_import_test_warn( recwarn: WarningsRecorder, module_qual_name: str, *, attribute_name: str = None, warn_nr=1 ): """Helper for testing changed imports, returning the imported item. Parameters ---------- module_qual_name : str Fully qualified name for a module e.g. ``glotaran.model.base_model`` attribute_name : str, optional Name of the attribute e.g. ``Model`` Returns ------- Any Module attribute or module """ warnings.simplefilter("always") recwarn.clear() if attribute_name is not None: result = module_attribute(module_qual_name, attribute_name) else: result = import_module(module_qual_name) check_recwarn(recwarn, warn_nr=warn_nr) return result
def check_recwarn(records: WarningsRecorder, warn_nr=1): for record in records: print(record) assert len(records) == warn_nr assert records[0].category == GlotaranApiDeprecationWarning records.clear()
def test_examples_sequential(recwarn: WarningsRecorder, attribute_name: str): """glotaran.examples.sequential exported attributes""" from glotaran.examples import sequential # noqa: F401 recwarn.clear() changed_import_test_warn( recwarn, "glotaran.examples.sequential", attribute_name=attribute_name )
def test_recording(self): rec = WarningsRecorder() with rec: assert not rec.list warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" values = rec.list rec.clear() assert len(rec.list) == 0 assert values is rec.list pytest.raises(AssertionError, "rec.pop()")
def test_recording(self): rec = WarningsRecorder() with rec: assert not rec.list warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" values = rec.list rec.clear() assert len(rec.list) == 0 assert values is rec.list pytest.raises(AssertionError, rec.pop)
def test_WarningRecorder(recwarn): showwarning = py.std.warnings.showwarning rec = WarningsRecorder() assert py.std.warnings.showwarning != showwarning assert not rec.list py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 py.std.warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" l = rec.list rec.clear() assert len(rec.list) == 0 assert l is rec.list pytest.raises(AssertionError, "rec.pop()") rec.finalize() assert showwarning == py.std.warnings.showwarning
def test_recording(self, recwarn): showwarning = py.std.warnings.showwarning rec = WarningsRecorder() with rec: assert py.std.warnings.showwarning != showwarning assert not rec.list py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 py.std.warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" l = rec.list rec.clear() assert len(rec.list) == 0 assert l is rec.list pytest.raises(AssertionError, "rec.pop()") assert showwarning == py.std.warnings.showwarning