class_docstr: This data container generalizes the typing when instantiating pyarrow arrays, tables or batches.
  
  More specifically it adds several features:
  - Support extension types like ``datasets.features.Array2DExtensionType``:
      By default pyarrow arrays don't return extension arrays. One has to call
      ``pa.ExtensionArray.from_storage(type, pa.array(data, type.storage_type))``
      in order to get an extension array.
  - Support for ``try_type`` parameter that can be used instead of ``type``:
      When an array is transformed, we like to keep the same type as before if possible.
      For example when calling :func:`datasets.Dataset.map`, we don't want to change the type
      of each column by default.
  - Better error message when a pyarrow array overflows.
  
  Example::
  
      from datasets.features import Array2D, Array2DExtensionType, Value
      from datasets.arrow_writer import TypedSequence
      import pyarrow as pa
  
      arr = pa.array(TypedSequence([1, 2, 3], type=Value("int32")))
      assert arr.type == pa.int32()
  
      arr = pa.array(TypedSequence([1, 2, 3], try_type=Value("int32")))
      assert arr.type == pa.int32()
  
      arr = pa.array(TypedSequence(["foo", "bar"], try_type=Value("int32")))
      assert arr.type == pa.string()
  
      arr = pa.array(TypedSequence([[[1, 2, 3]]], type=Array2D((1, 3), "int64")))
      assert arr.type == Array2DExtensionType((1, 3), "int64")
  
      table = pa.Table.from_pydict({
          "image": TypedSequence([[[1, 2, 3]]], type=Array2D((1, 3), "int64"))
      })
      assert table["image"].type == Array2DExtensionType((1, 3), "int64")
class_docstr_tok: ["This","data","container","generalizes","the","typing","when","instantiating","pyarrow","arrays",",","tables","or","batches",".","More","specifically","it","adds","several","features",":","-","Support","extension","types","like","``","datasets.features.Array2DExtensionType","``",":","By","default","pyarrow","arrays","do","n't","return","extension","arrays",".","One","has","to","call","``","pa.ExtensionArray.from_storage","(","type",",","pa.array","(","data",",","type.storage_type",")",")","``","in","order","to","get","an","extension","array",".","-","Support","for","``","try_type","``","parameter","that","can","be","used","instead","of","``","type","``",":","When","an","array","is","transformed",",","we","like","to","keep","the","same","type","as","before","if","possible",".","For","example","when","calling",":","func",":","`","datasets.Dataset.map","`",",","we","do","n't","want","to","change","the","type","of","each","column","by","default",".","-","Better","error","message","when","a","pyarrow","array","overflows",".","Example",":",":","from","datasets.features","import","Array2D",",","Array2DExtensionType",",","Value","from","datasets.arrow_writer","import","TypedSequence","import","pyarrow","as","pa","arr","=","pa.array","(","TypedSequence","(","[","1",",","2",",","3","]",",","type=Value","(","``","int32","''",")",")",")","assert","arr.type","==","pa.int32","(",")","arr","=","pa.array","(","TypedSequence","(","[","1",",","2",",","3","]",",","try_type=Value","(","``","int32","''",")",")",")","assert","arr.type","==","pa.int32","(",")","arr","=","pa.array","(","TypedSequence","(","[","``","foo","''",",","``","bar","''","]",",","try_type=Value","(","``","int32","''",")",")",")","assert","arr.type","==","pa.string","(",")","arr","=","pa.array","(","TypedSequence","(","[","[","[","1",",","2",",","3","]","]","]",",","type=Array2D","(","(","1",",","3",")",",","``","int64","''",")",")",")","assert","arr.type","==","Array2DExtensionType","(","(","1",",","3",")",",","``","int64","''",")","table","=","pa.Table.from_pydict","(","{","``","image","''",":","TypedSequence","(","[","[","[","1",",","2",",","3","]","]","]",",","type=Array2D","(","(","1",",","3",")",",","``","int64","''",")",")","}",")","assert","table","[","``","image","''","]",".type","==","Array2DExtensionType","(","(","1",",","3",")",",","``","int64","''",")"]
docstr: Implement type inference for custom objects like PIL.Image.Image -> Image type.
  
  This function is only used for custom python objects that can't be direclty passed to build
  an Arrow array. In such cases is infers the feature type to use, and it encodes the data so
  that they can be passed to an Arrow array.
  
  Args:
      data (Iterable): array of data to infer the type, e.g. a list of PIL images.
  
  Returns:
      Tuple[Iterable, Optional[FeatureType]]: a tuple with:
          - the (possibly encoded) array, if the inferred feature type requires encoding
          - the inferred feature type if the array is made of supported custom objects like
              PIL images, else None.
docstr_tok: ["Implement","type","inference","for","custom","objects","like","PIL.Image.Image","-",">","Image","type",".","This","function","is","only","used","for","custom","python","objects","that","ca","n't","be","direclty","passed","to","build","an","Arrow","array",".","In","such","cases","is","infers","the","feature","type","to","use",",","and","it","encodes","the","data","so","that","they","can","be","passed","to","an","Arrow","array",".","Args",":","data","(","Iterable",")",":","array","of","data","to","infer","the","type",",","e.g",".","a","list","of","PIL","images",".","Returns",":","Tuple","[","Iterable",",","Optional","[","FeatureType","]","]",":","a","tuple","with",":","-","the","(","possibly","encoded",")","array",",","if","the","inferred","feature","type","requires","encoding","-","the","inferred","feature","type","if","the","array","is","made","of","supported","custom","objects","like","PIL","images",",","else","None","."]
code: def _infer_custom_type_and_encode(
      data: Iterable,
  ) -> Tuple[Iterable, Optional[FeatureType]]:
      """Implement type inference for custom objects like PIL.Image.Image -> Image type.
  
      This function is only used for custom python objects that can't be direclty passed to build
      an Arrow array. In such cases is infers the feature type to use, and it encodes the data so
      that they can be passed to an Arrow array.
  
      Args:
          data (Iterable): array of data to infer the type, e.g. a list of PIL images.
  
      Returns:
          Tuple[Iterable, Optional[FeatureType]]: a tuple with:
              - the (possibly encoded) array, if the inferred feature type requires encoding
              - the inferred feature type if the array is made of supported custom objects like
                  PIL images, else None.
      """
      if config.PIL_AVAILABLE and "PIL" in sys.modules:
          import PIL.Image
  
          non_null_idx, non_null_value = first_non_null_value(data)
          if isinstance(non_null_value, PIL.Image.Image):
              return [
                  Image().encode_example(value)
                  if value is not None
                  else None
                  for value in data
              ], Image()
      return data, None
  
imports: import errno
  import json
  import os
  import sys
  from pathlib import Path
  from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
  import fsspec
  import numpy
  import pyarrow
  import pyarrow.parquet
  from .None import config
  from .features import Features, Image, Value
  from .features.features import FeatureType, _ArrayXDExtensionType, cast_to_python_objects, generate_from_arrow_type, get_nested_type, list_of_np_array_to_pyarrow_listarray, numpy_to_pyarrow_listarray, to_pyarrow_listarray
  from .filesystems import is_remote_filesystem
  from .info import DatasetInfo
  from .keyhash import DuplicatedKeysError, KeyHasher
  from .table import array_cast, array_concat, cast_array_to_feature, embed_table_storage, table_cast
  from .utils import logging
  from .utils import tqdm
  from .utils.file_utils import hash_url_to_filename
  from .utils.py_utils import asdict, first_non_null_value
label_desc: Use image node_id 3 for calling the TypedSequence obj's underlying member method code with example usage: obj._infer_custom_type_and_encode(data) and returns: data,  None, unknown,  Image