|
|
|
@ -17,7 +17,7 @@ import numpy as np
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
import mindspore.nn as nn
|
|
|
|
|
from mindspore import Tensor
|
|
|
|
|
from mindspore import Tensor, Parameter
|
|
|
|
|
from mindspore import context
|
|
|
|
|
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
|
|
|
|
@ -34,12 +34,14 @@ def test_isinstance():
|
|
|
|
|
self.tensor_member = Tensor(np.arange(4))
|
|
|
|
|
self.tuple_member = (1, 1.0, True, "abcd", self.tensor_member)
|
|
|
|
|
self.list_member = list(self.tuple_member)
|
|
|
|
|
self.weight = Parameter(1.0)
|
|
|
|
|
|
|
|
|
|
def construct(self, x, y):
|
|
|
|
|
is_int = isinstance(self.int_member, int)
|
|
|
|
|
is_float = isinstance(self.float_member, float)
|
|
|
|
|
is_bool = isinstance(self.bool_member, bool)
|
|
|
|
|
is_string = isinstance(self.string_member, str)
|
|
|
|
|
is_parameter = isinstance(self.weight, Parameter)
|
|
|
|
|
is_tensor_const = isinstance(self.tensor_member, Tensor)
|
|
|
|
|
is_tensor_var = isinstance(x, Tensor)
|
|
|
|
|
is_tuple_const = isinstance(self.tuple_member, tuple)
|
|
|
|
@ -52,7 +54,7 @@ def test_isinstance():
|
|
|
|
|
bool_is_string = isinstance(self.bool_member, str)
|
|
|
|
|
tensor_is_tuple = isinstance(x, tuple)
|
|
|
|
|
tuple_is_list = isinstance(self.tuple_member, list)
|
|
|
|
|
return is_int, is_float, is_bool, is_string, is_tensor_const, is_tensor_var, \
|
|
|
|
|
return is_int, is_float, is_bool, is_string, is_parameter, is_tensor_const, is_tensor_var, \
|
|
|
|
|
is_tuple_const, is_tuple_var, is_list_const, is_list_var, \
|
|
|
|
|
is_int_or_float_or_tensor_or_tuple, is_list_or_tensor, \
|
|
|
|
|
float_is_int, bool_is_string, tensor_is_tuple, tuple_is_list
|
|
|
|
@ -60,7 +62,7 @@ def test_isinstance():
|
|
|
|
|
net = Net()
|
|
|
|
|
x = Tensor(np.arange(4))
|
|
|
|
|
y = Tensor(np.arange(5))
|
|
|
|
|
assert net(x, y) == (True,) * 12 + (False,) * 4
|
|
|
|
|
assert net(x, y) == (True,) * 13 + (False,) * 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_isinstance_not_supported():
|
|
|
|
@ -76,3 +78,18 @@ def test_isinstance_not_supported():
|
|
|
|
|
with pytest.raises(TypeError) as err:
|
|
|
|
|
net()
|
|
|
|
|
assert "The type 'None' is not supported for 'isinstance'" in str(err.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_isinstance_second_arg_is_list():
|
|
|
|
|
class Net(nn.Cell):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Net, self).__init__()
|
|
|
|
|
self.value = (11, 22, 33, 44)
|
|
|
|
|
|
|
|
|
|
def construct(self):
|
|
|
|
|
return isinstance(self.value, [tuple, int, float])
|
|
|
|
|
|
|
|
|
|
net = Net()
|
|
|
|
|
with pytest.raises(TypeError) as err:
|
|
|
|
|
net()
|
|
|
|
|
assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a list" in str(err.value)
|
|
|
|
|