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 <derekbailey@google.com>
This commit is contained in:
Ben Beasley
2023-01-10 14:30:30 -05:00
committed by GitHub
parent b17d59b18c
commit 4e75867bd2

View File

@@ -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(),