| 739 | |
| 740 | @test_util.run_deprecated_v1 |
| 741 | def testGetAttr(self): |
| 742 | op = test_ops.default_attrs() |
| 743 | self.assertEqual(op.get_attr("string_val"), b"abc") |
| 744 | self.assertEqual(op.get_attr("string_list_val"), [b"abc", b""]) |
| 745 | self.assertEqual(op.get_attr("int_val"), 123) |
| 746 | self.assertEqual(op.get_attr("int_list_val"), [1, 2, 3]) |
| 747 | self.assertEqual(op.get_attr("float_val"), 10.0) |
| 748 | self.assertEqual(op.get_attr("float_list_val"), [10.0]) |
| 749 | self.assertEqual(op.get_attr("bool_val"), True) |
| 750 | self.assertEqual(op.get_attr("bool_list_val"), [True, False]) |
| 751 | self.assertEqual(op.get_attr("shape_val"), |
| 752 | tensor_shape.as_shape([2, 1]).as_proto()) |
| 753 | self.assertEqual(op.get_attr("shape_list_val"), |
| 754 | [tensor_shape.as_shape([]).as_proto(), |
| 755 | tensor_shape.as_shape([1]).as_proto()]) |
| 756 | self.assertEqual(op.get_attr("tensor_val"), |
| 757 | tensor_util.make_tensor_proto(1, dtypes.int32)) |
| 758 | self.assertEqual(op.get_attr("tensor_list_val"), |
| 759 | [tensor_util.make_tensor_proto(1, dtypes.int32)]) |
| 760 | |
| 761 | type_val = op.get_attr("type_val") |
| 762 | # First check that type_val is a DType, because the assertEquals will work |
| 763 | # no matter what since DType overrides __eq__ |
| 764 | self.assertIsInstance(type_val, dtypes.DType) |
| 765 | self.assertEqual(type_val, dtypes.int32) |
| 766 | |
| 767 | type_list_val = op.get_attr("type_list_val") |
| 768 | self.assertTrue(all(isinstance(x, dtypes.DType) for x in type_list_val)) |
| 769 | self.assertEqual(type_list_val, [dtypes.int32, dtypes.float32]) |
| 770 | |
| 771 | @function.Defun(dtypes.float32, func_name="MyFunc") |
| 772 | def func(x): |
| 773 | return x |
| 774 | |
| 775 | op = test_ops.func_attr(func) |
| 776 | self.assertEqual(op.get_attr("f"), |
| 777 | attr_value_pb2.NameAttrList(name="MyFunc")) |
| 778 | |
| 779 | # Try fetching missing attr |
| 780 | with self.assertRaisesRegexp( |
| 781 | ValueError, "Operation 'FuncAttr' has no attr named 'FakeAttr'."): |
| 782 | op.get_attr("FakeAttr") |
| 783 | |
| 784 | # TODO(b/65162920): remove this test when users who are directly mutating the |
| 785 | # node_def have been updated to proper usage. |