code: def test_write_tfrecords_empty_features(
      with_tf_schema,
      ray_start_regular_shared,
      tmp_path,
  ):
      """Test that write_tfrecords writes TFRecords with completely empty features
      correctly (i.e. the case where type inference from partially filled features
      is not possible). We expect this to succeed when passing an explicit `tf_schema`
      param, and otherwise will raise a `ValueError`.
  
      Test this by writing a Dataset to a TFRecord (function under test),
      reading it back out into a tf.train.Example,
      and checking that the result is analogous to the original Dataset.
      """
  
      import tensorflow as tf
  
      # The dataset we will write to a .tfrecords file.
      ds = ray.data.from_items(data_empty(with_tf_schema))
  
      # The corresponding tf.train.Example that we would expect to read
      # from this dataset.
      expected_records = tf_records_empty()
  
      if not with_tf_schema:
          with pytest.raises(ValueError):
              # Type inference from fully empty columns should fail if
              # no schema is specified.
              ds.write_tfrecords(tmp_path)
      else:
          features = expected_records[0].features
          tf_schema = _features_to_schema(features)
  
          # Perform the test.
          # Write the dataset to a .tfrecords file.
          ds.write_tfrecords(tmp_path, tf_schema=tf_schema)
  
          # Read the Examples back out from the .tfrecords file.
          # This follows the offical TFRecords tutorial:
          # https://www.tensorflow.org/tutorials/load_data/tfrecord#reading_a_tfrecord_file_2
  
          filenames = sorted(os.listdir(tmp_path))
          filepaths = [
              os.path.join(tmp_path, filename) for filename in filenames
          ]
          raw_dataset = tf.data.TFRecordDataset(filepaths)
  
          tfrecords = []
          for raw_record in raw_dataset:
              example = tf.train.Example()
              example.ParseFromString(raw_record.numpy())
              tfrecords.append(example)
  
          assert tfrecords == expected_records
  
imports: import json
  import os
  from typing import TYPE_CHECKING
  import numpy
  import pytest
  from pandas.api.types import is_float_dtype, is_int64_dtype, is_object_dtype
  import ray
  from ray.tests.conftest import *
label_desc: Use image node_id 10 for calling a global function with example usage: test_write_tfrecords_empty_features(with_tf_schema, ray_start_regular_shared, tmp_path) without return types