class_docstr: Tests FeatureExpectations with full encode-decode.
docstr: Test that encode=>decoding of a value works correctly.
docstr_tok: ["Test","that","encode=",">","decoding","of","a","value","works","correctly","."]
code: def assertFeatureTest(
      self,
      serialize_fdict,
      deserialize_fdict,
      test,
      feature,
      shape,
      dtype,
      test_tensor_spec: bool = True,
  ):
      """Test that encode=>decoding of a value works correctly."""
      # test feature.encode_example can be pickled and unpickled for beam.
      dill.loads(dill.dumps(feature.encode_example))
  
      input_value = {"inner": test.value}
  
      if test.raise_cls is not None or test.raise_cls_np is not None:
          if test.raise_cls is not None:
              with self._subTest("raise"):
                  if not test.raise_msg:
                      raise ValueError(
                          "test.raise_msg should be set with {} for test {}".format(
                              test.raise_cls, type(feature)
                          )
                      )
                  with self.assertRaisesWithPredicateMatch(
                      test.raise_cls, test.raise_msg
                  ):
                      features_encode_decode(
                          serialize_fdict,
                          deserialize_fdict,
                          input_value,
                          decoders=test.decoders,
                      )
          if test.raise_cls_np is not None:
              with self._subTest("raise_np"):
                  if not test.raise_msg:
                      raise ValueError(
                          "test.raise_msg should be set with {} for test {}".format(
                              test.raise_cls_np, type(feature)
                          )
                      )
                  with self.assertRaisesWithPredicateMatch(
                      test.raise_cls_np, test.raise_msg
                  ):
                      features_encode_decode_np(
                          serialize_fdict,
                          deserialize_fdict,
                          input_value,
                          decoders=test.decoders,
                      )
      else:
          # Test the serialization only
          if test.expected_serialized is not None:
              with self._subTest("out_serialize"):
                  self.assertEqual(
                      test.expected_serialized,
                      feature.encode_example(test.value),
                  )
  
          # Test serialization + decoding from disk for NumPy worflow
          if test.expected_np is not None:
              with self._subTest("out_np"):
                  out_numpy = features_encode_decode_np(
                      serialize_fdict,
                      deserialize_fdict,
                      input_value,
                      decoders={"inner": test.decoders},
                  )
                  with self._subTest("out_np_value"):
                      self.assertAllEqualNested(
                          out_numpy["inner"], test.expected_np
                      )
  
          # Test serialization + decoding from disk
          with self._subTest("out"):
              (
                  out_tensor,
                  out_numpy,
                  out_element_spec,
              ) = features_encode_decode(
                  serialize_fdict,
                  deserialize_fdict,
                  input_value,
                  decoders={"inner": test.decoders},
              )
              out_tensor = out_tensor["inner"]
              out_numpy = out_numpy["inner"]
              out_element_spec = out_element_spec["inner"]
  
              if test_tensor_spec:
                  with self._subTest("tensor_spec"):
                      assert (
                          feature.get_tensor_spec() == out_element_spec
                      )
  
              # Assert the returned type match the expected one
              with self._subTest("dtype"):
  
                  def _get_dtype(s):
                      if isinstance(s, tf.data.Dataset):
                          return tf.nest.map_structure(
                              _get_dtype, s.element_spec
                          )
                      else:
                          return s.dtype
  
                  out_dtypes = tf.nest.map_structure(
                      _get_dtype, out_tensor
                  )
                  self.assertEqual(
                      out_dtypes, test.dtype or feature.dtype
                  )
              with self._subTest("shape"):
                  # For shape, because (None, 3) match with (5, 3), we use
                  # tf.TensorShape.assert_is_compatible_with on each of the elements
                  expected_shape = (
                      feature.shape
                      if test.shape is None
                      else test.shape
                  )
  
                  def _get_shape(s):
                      if isinstance(s, tf.data.Dataset):
                          return utils.map_nested(
                              _get_shape, s.element_spec
                          )
                      else:
                          return s.shape
  
                  out_shapes = utils.map_nested(_get_shape, out_tensor)
  
                  shapes_tuple = utils.zip_nested(
                      out_shapes, expected_shape
                  )
                  utils.map_nested(
                      lambda x: x[0].assert_is_compatible_with(x[1]),
                      shapes_tuple,
                  )
  
              # Assert value
              with self._subTest("out_value"):
                  # Eventually construct the tf.RaggedTensor
                  expected = tf.nest.map_structure(
                      lambda t: t.build()
                      if isinstance(t, RaggedConstant)
                      else t,
                      test.expected,
                  )
                  self.assertAllEqualNested(
                      out_numpy, expected, atol=test.atol
                  )
  
              # Assert the HTML representation works
              if not test.decoders:
                  with self._subTest("repr"):
                      self._test_repr(feature, out_numpy)
  
imports: import contextlib
  import dataclasses
  import functools
  from typing import Any, Optional, Type
  import dill
  from etils import enp
  import numpy
  from tensorflow_datasets.core import dataset_utils
  from tensorflow_datasets.core import features
  from tensorflow_datasets.core import utils
  from tensorflow_datasets.core.data_sources import python
  from tensorflow_datasets.core.features import feature
  from tensorflow_datasets.core.utils import tree_utils
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
  from tensorflow_datasets.testing import test_case
  from tensorflow_datasets.testing import test_utils
label_desc: Use image node_id 5 for calling the FeatureExpectationsTestCase obj's underlying member method code with example usage: obj.assertFeatureTest(serialize_fdict, deserialize_fdict, test, feature, shape, dtype, test_tensor_spec) and returns: tf, s, utils, s