File assert_no_warning.patch of Package python-pydicom

From 37c0f957dcadebc93e6572a338e06150f7397732 Mon Sep 17 00:00:00 2001
From: mrbean-bremen <mrbean-bremen@users.noreply.github.com>
Date: Wed, 30 Mar 2022 00:01:39 +0200
Subject: [PATCH] Do not use pytest.warns(None) (#1623)

- avoids deprecation warnings from pytest
- fixes #1621
---
 pydicom/tests/test_dataset.py      | 27 ++++++++++-----------------
 pydicom/tests/test_filewriter.py   |  4 ++--
 pydicom/tests/test_handler_util.py |  7 +++----
 pydicom/tests/test_helpers.py      | 16 ++++++++++++++++
 4 files changed, 31 insertions(+), 23 deletions(-)
 create mode 100644 pydicom/tests/test_helpers.py

diff --git a/pydicom/tests/test_dataset.py b/pydicom/tests/test_dataset.py
index 89e42deff7..1e487ce21d 100644
--- a/pydicom/tests/test_dataset.py
+++ b/pydicom/tests/test_dataset.py
@@ -9,6 +9,7 @@
 import weakref
 
 import pytest
+from pydicom.tests.test_helpers import assert_no_warning
 
 try:
     import numpy
@@ -247,9 +248,8 @@ def test_contains_raises(self, contains_raise):
 
     def test_contains_ignore(self, contains_ignore):
         """Test ignoring invalid keys."""
-        with pytest.warns(None) as record:
+        with assert_no_warning():
             assert 'invalid' not in self.ds
-            assert len(record) == 0
 
     def test_clear(self):
         assert 1 == len(self.ds)
@@ -2210,15 +2210,13 @@ def setattr_warn():
 
 def test_setattr_warns(setattr_warn):
     """"Test warnings for Dataset.__setattr__() for close matches."""
-    with pytest.warns(None) as record:
+    with assert_no_warning():
         ds = Dataset()
-        assert len(record) == 0
 
     for s in CAMEL_CASE[0]:
-        with pytest.warns(None) as record:
+        with assert_no_warning():
             val = getattr(ds, s, None)
             setattr(ds, s, val)
-            assert len(record) == 0
 
     for s in CAMEL_CASE[1]:
         msg = (
@@ -2232,15 +2230,13 @@ def test_setattr_warns(setattr_warn):
 
 def test_setattr_raises(setattr_raise):
     """"Test exceptions for Dataset.__setattr__() for close matches."""
-    with pytest.warns(None) as record:
+    with assert_no_warning():
         ds = Dataset()
-        assert len(record) == 0
 
     for s in CAMEL_CASE[0]:
-        with pytest.warns(None) as record:
+        with assert_no_warning():
             val = getattr(ds, s, None)
             setattr(ds, s, val)
-            assert len(record) == 0
 
     for s in CAMEL_CASE[1]:
         msg = (
@@ -2254,19 +2250,16 @@ def test_setattr_raises(setattr_raise):
 
 def test_setattr_ignore(setattr_ignore):
     """Test config.INVALID_KEYWORD_BEHAVIOR = 'IGNORE'"""
-    with pytest.warns(None) as record:
+    with assert_no_warning():
         ds = Dataset()
-        assert len(record) == 0
 
     for s in CAMEL_CASE[0]:
-        with pytest.warns(None) as record:
+        with assert_no_warning():
             val = getattr(ds, s, None)
             setattr(ds, s, val)
-            assert len(record) == 0
 
     ds = Dataset()
     for s in CAMEL_CASE[1]:
-        with pytest.warns(None) as record:
-            val = getattr(ds, s, None)
+        with assert_no_warning():
+            getattr(ds, s, None)
             setattr(ds, s, None)
-            assert len(record) == 0
diff --git a/pydicom/tests/test_filewriter.py b/pydicom/tests/test_filewriter.py
index 1adbe0cdb1..584bf55a5d 100644
--- a/pydicom/tests/test_filewriter.py
+++ b/pydicom/tests/test_filewriter.py
@@ -28,6 +28,7 @@
 )
 from pydicom.multival import MultiValue
 from pydicom.sequence import Sequence
+from pydicom.tests.test_helpers import assert_no_warning
 from pydicom.uid import (
     ImplicitVRLittleEndian,
     ExplicitVRBigEndian,
@@ -2435,9 +2436,8 @@ def test_single_byte_multi_charset_groups(self):
         # PersonName value has not saved the default encoding
         fp = DicomBytesIO()
         fp.is_little_endian = True
-        with pytest.warns(None) as warnings:
+        with assert_no_warning():
             write_PN(fp, elem, encodings)
-        assert not warnings
         assert encoded == fp.getvalue()
 
         fp = DicomBytesIO()
diff --git a/pydicom/tests/test_handler_util.py b/pydicom/tests/test_handler_util.py
index 81fecd9ac0..62187be84f 100644
--- a/pydicom/tests/test_handler_util.py
+++ b/pydicom/tests/test_handler_util.py
@@ -7,6 +7,7 @@
 from sys import byteorder
 
 import pytest
+from pydicom.tests.test_helpers import assert_no_warning
 
 try:
     import numpy as np
@@ -2234,17 +2235,15 @@ def test_missing(self):
         """Test return value when (0028,0008) 'Number of Frames' does not
             exist"""
         ds = Dataset()
-        with pytest.warns(None) as w:
+        with assert_no_warning():
             assert 1 == get_nr_frames(ds)
-            assert not w
 
     def test_existing(self):
         """Test return value when (0028,0008) 'Number of Frames' exists."""
         ds = Dataset()
         ds.NumberOfFrames = random.randint(1, 10)
-        with pytest.warns(None) as w:
+        with assert_no_warning():
             assert ds.NumberOfFrames == get_nr_frames(ds)
-            assert not w
 
 
 REFERENCE_PACK_UNPACK = [
diff --git a/pydicom/tests/test_helpers.py b/pydicom/tests/test_helpers.py
new file mode 100644
index 0000000000..a0cc491835
--- /dev/null
+++ b/pydicom/tests/test_helpers.py
@@ -0,0 +1,16 @@
+# Copyright 2008-2018 pydicom authors. See LICENSE file for details.
+"""Helper functions for tests."""
+
+import warnings
+from contextlib import contextmanager
+from typing import Generator
+
+
+@contextmanager
+def assert_no_warning() -> Generator:
+    """Assert that no warning is issued.
+    Any warning will be handled as an error.
+    """
+    with warnings.catch_warnings():
+        warnings.simplefilter("error")
+        yield
openSUSE Build Service is sponsored by