You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_isinstance.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"""
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. def test_isinstance():
  23. class Net(nn.Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.int_member = 1
  27. self.float_member = 1.0
  28. self.bool_member = True
  29. self.string_member = "abcd"
  30. self.tensor_member = Tensor(np.arange(4))
  31. self.tuple_member = (1, 1.0, True, "abcd", self.tensor_member)
  32. self.list_member = list(self.tuple_member)
  33. self.weight = Parameter(1.0)
  34. self.empty_list = []
  35. def construct(self, x, y):
  36. is_int = isinstance(self.int_member, int)
  37. is_float = isinstance(self.float_member, float)
  38. is_bool = isinstance(self.bool_member, bool)
  39. is_string = isinstance(self.string_member, str)
  40. is_parameter = isinstance(self.weight, Parameter)
  41. is_tensor_const = isinstance(self.tensor_member, Tensor)
  42. is_tensor_var = isinstance(x, Tensor)
  43. is_tuple_const = isinstance(self.tuple_member, tuple)
  44. is_tuple_var = isinstance((x, 1, 1.0, y), tuple)
  45. is_list_const = isinstance(self.list_member, list)
  46. is_list_var = isinstance([x, 1, 1.0, y], list)
  47. is_list_or_tensor = isinstance([x, y], (Tensor, list))
  48. is_int_or_float_or_tensor_or_tuple = isinstance(x, (Tensor, tuple, int, float))
  49. float_is_int = isinstance(self.float_member, int)
  50. bool_is_string = isinstance(self.bool_member, str)
  51. tensor_is_tuple = isinstance(x, tuple)
  52. tuple_is_list = isinstance(self.tuple_member, list)
  53. is_empty_list = isinstance(self.empty_list, list)
  54. return is_int, is_float, is_bool, is_string, \
  55. is_empty_list, is_parameter, is_tensor_const, is_tensor_var, \
  56. is_tuple_const, is_tuple_var, is_list_const, is_list_var, \
  57. is_int_or_float_or_tensor_or_tuple, is_list_or_tensor, \
  58. float_is_int, bool_is_string, tensor_is_tuple, tuple_is_list
  59. net = Net()
  60. x = Tensor(np.arange(4))
  61. y = Tensor(np.arange(5))
  62. assert net(x, y) == (True,) * 14 + (False,) * 4
  63. def test_isinstance_not_supported():
  64. class Net(nn.Cell):
  65. def __init__(self):
  66. super(Net, self).__init__()
  67. self.value = (11, 22, 33, 44)
  68. def construct(self):
  69. return isinstance(self.value, None)
  70. net = Net()
  71. with pytest.raises(TypeError) as err:
  72. net()
  73. assert "The second arg of 'isinstance' should be bool, int, float, str, list, tuple, Tensor, Parameter, " \
  74. "or a tuple only including these types, but got None" in str(err.value)
  75. def test_isinstance_second_arg_is_list():
  76. class Net(nn.Cell):
  77. def __init__(self):
  78. super(Net, self).__init__()
  79. self.value = (11, 22, 33, 44)
  80. def construct(self):
  81. return isinstance(self.value, [tuple, int, float])
  82. net = Net()
  83. with pytest.raises(TypeError) as err:
  84. net()
  85. assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a list" in str(err.value)