code: def test_extract_features():
      features = features_lib.FeaturesDict(
          {
              "img": features_lib.Image(shape=(256, 256, 3)),
              "img2": features_lib.Image(shape=(256, 256, 3)),
              "metadata": {
                  "label": features_lib.ClassLabel(num_classes=4),
                  "other": tf.string,
              },
              "sequence": features_lib.Sequence(
                  {
                      "x": tf.int64,
                      "y": tf.int64,
                  }
              ),
              "sequence_flat": features_lib.Sequence(tf.int64),
          }
      )
  
      result = _extract_features(
          feature=features,
          expected_feature={},
      )
      testing.assert_features_equal(
          result, features_lib.FeaturesDict({})
      )
  
      # Feature spec accepted
      result = _extract_features(
          feature=features,
          expected_feature={
              "img": features_lib.Image(shape=(None, None, 3)),
              "metadata": {
                  "other": tf.string,
              },
              "sequence": features_lib.Sequence(
                  {
                      "x": tf.int64,
                  }
              ),
          },
      )
      testing.assert_features_equal(
          result,
          features_lib.FeaturesDict(
              {
                  "img": features_lib.Image(shape=(256, 256, 3)),
                  "metadata": {
                      "other": tf.string,
                  },
                  "sequence": features_lib.Sequence(
                      {
                          "x": tf.int64,
                      }
                  ),
              }
          ),
      )
  
      # Failure mode:
      # * Structure not matching
      # * Type not matching
      # * Shape/dtype not matching
      # * Sequence values not matching (e.g. try bad dtype)
  
      with pytest.raises(
          ValueError, match="Missing expected feature 'unknown'"
      ):
          _extract_features(
              feature=features,
              expected_feature={
                  "sequence": features_lib.Sequence(
                      {
                          "unknown": tf.bool,
                      }
                  )
              },
          )
  
      with pytest.raises(
          ValueError, match="Missing expected feature 'non_exista"
      ):
          _extract_features(
              feature=features,
              expected_feature={
                  "non_existant": features_lib.Image(
                      shape=(None, None, 3)
                  ),
              },
          )
  
      with pytest.raises(
          TypeError, match="Expected: Tensor.*. Got: Image"
      ):
          _extract_features(
              feature=features,
              expected_feature={
                  "img": features_lib.Tensor(
                      shape=(256, 256, 3), dtype=tf.uint8
                  ),
              },
          )
  
      with pytest.raises(
          ValueError, match="Expected: Image.*. Got: Image"
      ):
          _extract_features(
              feature=features,
              expected_feature={
                  "img": features_lib.Image(shape=(None, None, 1)),
              },
          )
  
      with pytest.raises(
          ValueError, match="Expected: Tensor.*. Got: Tensor"
      ):
          _extract_features(
              feature=features,
              expected_feature={
                  "sequence_flat": features_lib.Sequence(
                      tf.float32
                  ),  # Wrong dtype
              },
          )
  
file_path: datasets/tensorflow_datasets/core/decode/partial_decode_test.py
imports: import pytest
  import tensorflow
  from tensorflow_datasets import testing
  from tensorflow_datasets.core import decode
  from tensorflow_datasets.core import features
label_desc: Use image node_id 2 for calling a global function with example usage: test_extract_features() without return types