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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. self.dict_member = {"x": Tensor(np.arange(4)), "y": Tensor(np.arange(5))}
  36. self.empty_dict = {}
  37. def construct(self, x, y):
  38. is_int = isinstance(self.int_member, int)
  39. is_float = isinstance(self.float_member, float)
  40. is_bool = isinstance(self.bool_member, bool)
  41. bool_is_int = isinstance(self.bool_member, (((int,)), float))
  42. is_string = isinstance(self.string_member, str)
  43. is_parameter = isinstance(self.weight, Parameter)
  44. parameter_is_tensor = isinstance(self.weight, ((Tensor, float), int))
  45. is_tensor_const = isinstance(self.tensor_member, Tensor)
  46. is_tensor_var = isinstance(x, Tensor)
  47. is_tuple_const = isinstance(self.tuple_member, tuple)
  48. is_tuple_var = isinstance((x, 1, 1.0, y), tuple)
  49. is_list_const = isinstance(self.list_member, list)
  50. is_list_var = isinstance([x, 1, 1.0, y], list)
  51. is_dict_const = isinstance(self.dict_member, dict)
  52. is_dict_var = isinstance({"x": x, "y": y}, dict)
  53. is_empty_dic = isinstance(self.empty_dict, dict)
  54. is_list_or_tensor = isinstance([x, y], (Tensor, list))
  55. is_int_or_float_or_tensor_or_tuple = isinstance(x, (Tensor, tuple, int, float))
  56. float_is_int = isinstance(self.float_member, int)
  57. bool_is_string = isinstance(self.bool_member, str)
  58. tensor_is_tuple = isinstance(x, tuple)
  59. tuple_is_list = isinstance(self.tuple_member, list)
  60. is_empty_list = isinstance(self.empty_list, list)
  61. return is_int, is_float, is_bool, bool_is_int, is_string, is_parameter, \
  62. parameter_is_tensor, is_tensor_const, is_tensor_var, \
  63. is_tuple_const, is_tuple_var, is_list_const, is_list_var, is_empty_list, \
  64. is_dict_const, is_dict_var, is_empty_dic, \
  65. is_int_or_float_or_tensor_or_tuple, is_list_or_tensor, \
  66. float_is_int, bool_is_string, tensor_is_tuple, tuple_is_list
  67. net = Net()
  68. x = Tensor(np.arange(4))
  69. y = Tensor(np.arange(5))
  70. assert net(x, y) == (True,) * 19 + (False,) * 4
  71. def test_isinstance_not_supported():
  72. class Net(nn.Cell):
  73. def __init__(self):
  74. super(Net, self).__init__()
  75. self.value = (11, 22, 33, 44)
  76. def construct(self):
  77. return isinstance(self.value, None)
  78. net = Net()
  79. with pytest.raises(TypeError) as err:
  80. net()
  81. assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a NoneType" in str(err.value)
  82. def test_isinstance_second_arg_is_list():
  83. class Net(nn.Cell):
  84. def __init__(self):
  85. super(Net, self).__init__()
  86. self.value = (11, 22, 33, 44)
  87. def construct(self):
  88. return isinstance(self.value, [tuple, int, float])
  89. net = Net()
  90. with pytest.raises(TypeError) as err:
  91. net()
  92. assert "The second arg of 'isinstance' must be a type or a tuple of types, but got a list" in str(err.value)