From 4e75867bd2ad0f1c01aa5457713264a48d87339e Mon Sep 17 00:00:00 2001 From: Ben Beasley Date: Tue, 10 Jan 2023 14:30:30 -0500 Subject: [PATCH] Stop using deprecated imp package in Python tests (#7769) It is deprecated in favour of importlib and slated for removal in Python 3.12. Since the return value of imp.find_module('numpy') is unused, the only effect of calling this function is to raise an ImportError when numpy is not available; importing numpy directly is already sufficient to do this. The imp package is still used in python/flatbuffers/compat.py, but only on Python 2, where it is not deprecated and will not be removed. Co-authored-by: Derek Bailey --- tests/py_test.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/py_test.py b/tests/py_test.py index 672dd3201..0947adfa3 100644 --- a/tests/py_test.py +++ b/tests/py_test.py @@ -15,7 +15,6 @@ import os.path import sys -import imp PY_VERSION = sys.version_info[:2] import ctypes @@ -695,7 +694,6 @@ def CheckReadBuffer(buf, offset, sizePrefix=False, file_identifier=None): ])) try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1074,7 +1072,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_int8(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1120,7 +1117,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_uint16(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1174,7 +1170,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_int64(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1261,7 +1256,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_float32(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1324,7 +1318,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_float64(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1411,7 +1404,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_bool(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1458,7 +1450,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_reject_strings(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -1476,7 +1467,6 @@ class TestByteLayout(unittest.TestCase): def test_create_numpy_vector_reject_object(self): try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array import numpy as np @@ -2343,9 +2333,10 @@ class TestAllCodePathsOfExampleSchema(unittest.TestCase): self.assertEqual(2, mon2.Testnestedflatbuffer(1)) self.assertEqual(4, mon2.Testnestedflatbuffer(2)) try: - imp.find_module('numpy') # if numpy exists, then we should be able to get the # vector as a numpy array + import numpy as np + self.assertEqual([0, 2, 4], mon2.TestnestedflatbufferAsNumpy().tolist()) except ImportError: assertRaises(self, lambda: mon2.TestnestedflatbufferAsNumpy(),