• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15""" test instance"""
16import numpy as np
17import pytest
18
19import mindspore.nn as nn
20from mindspore import Tensor, Parameter
21from mindspore import context
22
23context.set_context(mode=context.GRAPH_MODE)
24
25
26def test_isinstance():
27    class Net(nn.Cell):
28        def __init__(self):
29            super(Net, self).__init__()
30            self.int_member = 1
31            self.float_member = 1.0
32            self.bool_member = True
33            self.string_member = "abcd"
34            self.tensor_member = Tensor(np.arange(4))
35            self.tuple_member = (1, 1.0, True, "abcd", self.tensor_member)
36            self.list_member = list(self.tuple_member)
37            self.weight = Parameter(1.0)
38            self.empty_list = []
39            self.dict_member = {"x": Tensor(np.arange(4)), "y": Tensor(np.arange(5))}
40            self.empty_dict = {}
41
42        def construct(self, x, y):
43            is_int = isinstance(self.int_member, int)
44            is_float = isinstance(self.float_member, float)
45            is_bool = isinstance(self.bool_member, bool)
46            bool_is_int = isinstance(self.bool_member, (((int,)), float))
47            is_string = isinstance(self.string_member, str)
48            is_parameter = isinstance(self.weight, Parameter)
49            parameter_is_tensor = isinstance(self.weight, ((Tensor, float), int))
50            is_tensor_const = isinstance(self.tensor_member, Tensor)
51            is_tensor_var = isinstance(x, Tensor)
52            is_tuple_const = isinstance(self.tuple_member, tuple)
53            is_tuple_var = isinstance((x, 1, 1.0, y), tuple)
54            is_list_const = isinstance(self.list_member, list)
55            is_list_var = isinstance([x, 1, 1.0, y], list)
56            is_dict_const = isinstance(self.dict_member, dict)
57            is_dict_var = isinstance({"x": x, "y": y}, dict)
58            is_empty_dic = isinstance(self.empty_dict, dict)
59            is_list_or_tensor = isinstance([x, y], (Tensor, list))
60            is_int_or_float_or_tensor_or_tuple = isinstance(x, (Tensor, tuple, int, float))
61            float_is_int = isinstance(self.float_member, int)
62            bool_is_string = isinstance(self.bool_member, str)
63            tensor_is_tuple = isinstance(x, tuple)
64            tuple_is_list = isinstance(self.tuple_member, list)
65            is_empty_list = isinstance(self.empty_list, list)
66            return is_int, is_float, is_bool, bool_is_int, is_string, is_parameter, \
67                   parameter_is_tensor, is_tensor_const, is_tensor_var, \
68                   is_tuple_const, is_tuple_var, is_list_const, is_list_var, is_empty_list, \
69                   is_dict_const, is_dict_var, is_empty_dic, \
70                   is_int_or_float_or_tensor_or_tuple, is_list_or_tensor, \
71                   float_is_int, bool_is_string, tensor_is_tuple, tuple_is_list
72
73    net = Net()
74    x = Tensor(np.arange(4))
75    y = Tensor(np.arange(5))
76    assert net(x, y) == (True,) * 19 + (False,) * 4
77
78
79def test_isinstance_not_supported():
80    class Net(nn.Cell):
81        def __init__(self):
82            super(Net, self).__init__()
83            self.value = (11, 22, 33, 44)
84
85        def construct(self):
86            return isinstance(self.value, None)
87
88    net = Net()
89    with pytest.raises(TypeError) as err:
90        net()
91    assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a NoneType" in str(err.value)
92
93
94def test_isinstance_second_arg_is_list():
95    class Net(nn.Cell):
96        def __init__(self):
97            super(Net, self).__init__()
98            self.value = (11, 22, 33, 44)
99
100        def construct(self):
101            return isinstance(self.value, [tuple, int, float])
102
103    net = Net()
104    with pytest.raises(TypeError) as err:
105        net()
106    assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a list" in str(err.value)
107