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 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  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. def construct(self, x, y):
  34. is_int = isinstance(self.int_member, int)
  35. is_float = isinstance(self.float_member, float)
  36. is_bool = isinstance(self.bool_member, bool)
  37. is_string = isinstance(self.string_member, str)
  38. is_tensor_const = isinstance(self.tensor_member, Tensor)
  39. is_tensor_var = isinstance(x, Tensor)
  40. is_tuple_const = isinstance(self.tuple_member, tuple)
  41. is_tuple_var = isinstance((x, 1, 1.0, y), tuple)
  42. is_list_const = isinstance(self.list_member, list)
  43. is_list_var = isinstance([x, 1, 1.0, y], list)
  44. is_list_or_tensor = isinstance([x, y], (Tensor, list))
  45. is_int_or_float_or_tensor_or_tuple = isinstance(x, (Tensor, tuple, int, float))
  46. float_is_int = isinstance(self.float_member, int)
  47. bool_is_string = isinstance(self.bool_member, str)
  48. tensor_is_tuple = isinstance(x, tuple)
  49. tuple_is_list = isinstance(self.tuple_member, list)
  50. return is_int, is_float, is_bool, is_string, is_tensor_const, is_tensor_var, \
  51. is_tuple_const, is_tuple_var, is_list_const, is_list_var, \
  52. is_int_or_float_or_tensor_or_tuple, is_list_or_tensor, \
  53. float_is_int, bool_is_string, tensor_is_tuple, tuple_is_list
  54. net = Net()
  55. x = Tensor(np.arange(4))
  56. y = Tensor(np.arange(5))
  57. assert net(x, y) == (True,) * 12 + (False,) * 4
  58. def test_isinstance_not_supported():
  59. class Net(nn.Cell):
  60. def __init__(self):
  61. super(Net, self).__init__()
  62. self.value = (11, 22, 33, 44)
  63. def construct(self):
  64. return isinstance(self.value, None)
  65. net = Net()
  66. with pytest.raises(TypeError) as err:
  67. net()
  68. assert "The type 'None' is not supported for 'isinstance'" in str(err.value)