



"""simple docstring"""
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


A:     str										=	get_tests_dir("fixtures/dummy_feature_extractor_config.json")
A:     Optional[Any]										=	get_tests_dir("fixtures/vocab.json")
A:     Optional[Any]										=	get_tests_dir("fixtures")



class    SCREAMING_SNAKE_CASE__						(      unittest.TestCase				):
   __lowerCAmelCase						:     Optional[Any]		=		['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou']

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




    UpperCAmelCase			:      Union[str, Any]			=			0

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   str:





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""		)
    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[int]:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase			:      Optional[Any]			=			WavaVecaConfig()
     UpperCAmelCase			:      List[Any]			=			AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""		)

     # save in new folder
     model_config.save_pretrained(_SCREAMING_SNAKE_CASE		)
     processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

     UpperCAmelCase			:      Optional[Any]			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[int]:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdirname:
     # copy relevant files
     copyfile(_SCREAMING_SNAKE_CASE   ,  os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)		)
     copyfile(_SCREAMING_SNAKE_CASE   ,  os.path.join(_SCREAMING_SNAKE_CASE   ,  """vocab.json"""		)		)

     UpperCAmelCase			:      str			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   int:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase			:      Dict			=			WavaVecaFeatureExtractor()
     UpperCAmelCase			:      Optional[int]			=			AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""		)

     UpperCAmelCase			:      Tuple			=			WavaVecaProcessor(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

     # save in new folder
     processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

     # drop `processor_class` in tokenizer
     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  """r"""		) as f:
      UpperCAmelCase			:      int			=			json.load(_SCREAMING_SNAKE_CASE		)
      config_dict.pop("""processor_class"""		)

     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  """w"""		) as f:
      f.write(json.dumps(_SCREAMING_SNAKE_CASE		)		)

     UpperCAmelCase			:      Optional[Any]			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase			:      Tuple			=			WavaVecaFeatureExtractor()
     UpperCAmelCase			:      Optional[int]			=			AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""		)

     UpperCAmelCase			:      List[Any]			=			WavaVecaProcessor(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

     # save in new folder
     processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

     # drop `processor_class` in feature extractor
     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  """r"""		) as f:
      UpperCAmelCase			:      Optional[int]			=			json.load(_SCREAMING_SNAKE_CASE		)
      config_dict.pop("""processor_class"""		)

     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  """w"""		) as f:
      f.write(json.dumps(_SCREAMING_SNAKE_CASE		)		)

     UpperCAmelCase			:      Union[str, Any]			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase			:      Union[str, Any]			=			WavaVecaConfig(processor_class="""Wav2Vec2Processor"""		)
     model_config.save_pretrained(_SCREAMING_SNAKE_CASE		)
     # copy relevant files
     copyfile(_SCREAMING_SNAKE_CASE   ,  os.path.join(_SCREAMING_SNAKE_CASE   ,  """vocab.json"""		)		)
     # create emtpy sample processor
     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)   ,  """w"""		) as f:
      f.write("""{}"""		)

     UpperCAmelCase			:      Dict			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Union[str, Any]:





    '''simple docstring'''




    with self.assertRaises(_SCREAMING_SNAKE_CASE		):
     UpperCAmelCase			:      Optional[Any]			=			AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""		)
    # If remote code is disabled, we can't load this config.
    with self.assertRaises(_SCREAMING_SNAKE_CASE		):
     UpperCAmelCase			:      List[str]			=			AutoProcessor.from_pretrained(
         """hf-internal-testing/test_dynamic_processor"""   ,  trust_remote_code=_SCREAMING_SNAKE_CASE		)

    UpperCAmelCase			:      Union[str, Any]			=			AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""   ,  trust_remote_code=_SCREAMING_SNAKE_CASE		)
    self.assertTrue(processor.special_attribute_present		)
    self.assertEqual(processor.__class__.__name__   ,  """NewProcessor"""		)

    UpperCAmelCase			:      Optional[int]			=			processor.feature_extractor
    self.assertTrue(feature_extractor.special_attribute_present		)
    self.assertEqual(feature_extractor.__class__.__name__   ,  """NewFeatureExtractor"""		)

    UpperCAmelCase			:      Union[str, Any]			=			processor.tokenizer
    self.assertTrue(tokenizer.special_attribute_present		)
    if is_tokenizers_available():
     self.assertEqual(tokenizer.__class__.__name__   ,  """NewTokenizerFast"""		)

     # Test we can also load the slow version
     UpperCAmelCase			:      Union[str, Any]			=			AutoProcessor.from_pretrained(
         """hf-internal-testing/test_dynamic_processor"""   ,  trust_remote_code=_SCREAMING_SNAKE_CASE   ,  use_fast=_SCREAMING_SNAKE_CASE		)
     UpperCAmelCase			:      Dict			=			new_processor.tokenizer
     self.assertTrue(new_tokenizer.special_attribute_present		)
     self.assertEqual(new_tokenizer.__class__.__name__   ,  """NewTokenizer"""		)
    else:
     self.assertEqual(tokenizer.__class__.__name__   ,  """NewTokenizer"""		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   str:





    '''simple docstring'''




    try:
     AutoConfig.register("""custom"""   ,  _SCREAMING_SNAKE_CASE		)
     AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
     AutoTokenizer.register(_SCREAMING_SNAKE_CASE   ,  slow_tokenizer_class=_SCREAMING_SNAKE_CASE		)
     AutoProcessor.register(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
     # Trying to register something existing in the Transformers library will raise an error
     with self.assertRaises(_SCREAMING_SNAKE_CASE		):
      AutoProcessor.register(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

     # Now that the config is registered, it can be used as any other config with the auto-API
     UpperCAmelCase			:      List[str]			=			CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE		)

     with tempfile.TemporaryDirectory() as tmp_dir:
      UpperCAmelCase			:      Union[str, Any]			=			os.path.join(_SCREAMING_SNAKE_CASE   ,  """vocab.txt"""		)
      with open(_SCREAMING_SNAKE_CASE   ,  """w"""   ,  encoding="""utf-8"""		) as vocab_writer:
       vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]		)		)
      UpperCAmelCase			:      Any			=			CustomTokenizer(_SCREAMING_SNAKE_CASE		)

     UpperCAmelCase			:      Union[str, Any]			=			CustomProcessor(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

     with tempfile.TemporaryDirectory() as tmp_dir:
      processor.save_pretrained(_SCREAMING_SNAKE_CASE		)
      UpperCAmelCase			:      Optional[Any]			=			AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

    finally:
     if "custom" in CONFIG_MAPPING._extra_content:
      del CONFIG_MAPPING._extra_content["custom"]
     if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
      del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
     if CustomConfig in TOKENIZER_MAPPING._extra_content:
      del TOKENIZER_MAPPING._extra_content[CustomConfig]
     if CustomConfig in PROCESSOR_MAPPING._extra_content:
      del PROCESSOR_MAPPING._extra_content[CustomConfig]

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[str]:





    '''simple docstring'''




    class    SCREAMING_SNAKE_CASE__						(      UpperCAmelCase__				):
       __lowerCAmelCase						:     List[Any]		=		False



    class    SCREAMING_SNAKE_CASE__						(      UpperCAmelCase__				):
       __lowerCAmelCase						:     Optional[Any]		=		False



    class    SCREAMING_SNAKE_CASE__						(      UpperCAmelCase__				):
       __lowerCAmelCase						:     Union[str, Any]		=		'AutoFeatureExtractor'
       __lowerCAmelCase						:     Union[str, Any]		=		'AutoTokenizer'
       __lowerCAmelCase						:     Optional[int]		=		False

    try:
     AutoConfig.register("""custom"""   ,  _SCREAMING_SNAKE_CASE		)
     AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
     AutoTokenizer.register(_SCREAMING_SNAKE_CASE   ,  slow_tokenizer_class=_SCREAMING_SNAKE_CASE		)
     AutoProcessor.register(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
     # If remote code is not set, the default is to use local classes.
     UpperCAmelCase			:      Optional[int]			=			AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""		)
     self.assertEqual(processor.__class__.__name__   ,  """NewProcessor"""		)
     self.assertFalse(processor.special_attribute_present		)
     self.assertFalse(processor.feature_extractor.special_attribute_present		)
     self.assertFalse(processor.tokenizer.special_attribute_present		)

     # If remote code is disabled, we load the local ones.
     UpperCAmelCase			:      Tuple			=			AutoProcessor.from_pretrained(
         """hf-internal-testing/test_dynamic_processor"""   ,  trust_remote_code=_SCREAMING_SNAKE_CASE		)
     self.assertEqual(processor.__class__.__name__   ,  """NewProcessor"""		)
     self.assertFalse(processor.special_attribute_present		)
     self.assertFalse(processor.feature_extractor.special_attribute_present		)
     self.assertFalse(processor.tokenizer.special_attribute_present		)

     # If remote is enabled, we load from the Hub.
     UpperCAmelCase			:      Tuple			=			AutoProcessor.from_pretrained(
         """hf-internal-testing/test_dynamic_processor"""   ,  trust_remote_code=_SCREAMING_SNAKE_CASE		)
     self.assertEqual(processor.__class__.__name__   ,  """NewProcessor"""		)
     self.assertTrue(processor.special_attribute_present		)
     self.assertTrue(processor.feature_extractor.special_attribute_present		)
     self.assertTrue(processor.tokenizer.special_attribute_present		)

    finally:
     if "custom" in CONFIG_MAPPING._extra_content:
      del CONFIG_MAPPING._extra_content["custom"]
     if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
      del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
     if CustomConfig in TOKENIZER_MAPPING._extra_content:
      del TOKENIZER_MAPPING._extra_content[CustomConfig]
     if CustomConfig in PROCESSOR_MAPPING._extra_content:
      del PROCESSOR_MAPPING._extra_content[CustomConfig]

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   List[str]:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-bert"""		)
    self.assertEqual(processor.__class__.__name__   ,  """BertTokenizerFast"""		)




   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Union[str, Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[int]			=			AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-convnext"""		)
    self.assertEqual(processor.__class__.__name__   ,  """ConvNextImageProcessor"""		)



@is_staging_test
class    SCREAMING_SNAKE_CASE__						(      unittest.TestCase				):
   __lowerCAmelCase						:     Dict		=		['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou']

   @classmethod
   def 			SCREAMING_SNAKE_CASE  (     cls		)						->   Optional[int]:





    '''simple docstring'''




    UpperCAmelCase			:      str			=			TOKEN
    HfFolder.save_token(_SCREAMING_SNAKE_CASE		)

   @classmethod
   def 			SCREAMING_SNAKE_CASE  (     cls		)						->   int:





    '''simple docstring'''




    try:
     delete_repo(token=cls._token   ,  repo_id="""test-processor"""		)
    except HTTPError:
     pass

    try:
     delete_repo(token=cls._token   ,  repo_id="""valid_org/test-processor-org"""		)
    except HTTPError:
     pass

    try:
     delete_repo(token=cls._token   ,  repo_id="""test-dynamic-processor"""		)
    except HTTPError:
     pass

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[int]:





    '''simple docstring'''




    UpperCAmelCase			:      Union[str, Any]			=			WavaVecaProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)
    with tempfile.TemporaryDirectory() as tmp_dir:
     processor.save_pretrained(
         os.path.join(_SCREAMING_SNAKE_CASE   ,  """test-processor"""		)   ,  push_to_hub=_SCREAMING_SNAKE_CASE   ,  use_auth_token=self._token		)

     UpperCAmelCase			:      List[str]			=			WavaVecaProcessor.from_pretrained(F"{USER}/test-processor"		)
     for k, v in processor.feature_extractor.__dict__.items():
      self.assertEqual(_SCREAMING_SNAKE_CASE   ,  getattr(new_processor.feature_extractor   ,  _SCREAMING_SNAKE_CASE		)		)
     self.assertDictEqual(new_processor.tokenizer.get_vocab()   ,  processor.tokenizer.get_vocab()		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[int]:





    '''simple docstring'''




    UpperCAmelCase			:      int			=			WavaVecaProcessor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    with tempfile.TemporaryDirectory() as tmp_dir:
     processor.save_pretrained(
         os.path.join(_SCREAMING_SNAKE_CASE   ,  """test-processor-org"""		)   ,  push_to_hub=_SCREAMING_SNAKE_CASE   ,  use_auth_token=self._token   ,  organization="""valid_org"""   ,  )

     UpperCAmelCase			:      str			=			WavaVecaProcessor.from_pretrained("""valid_org/test-processor-org"""		)
     for k, v in processor.feature_extractor.__dict__.items():
      self.assertEqual(_SCREAMING_SNAKE_CASE   ,  getattr(new_processor.feature_extractor   ,  _SCREAMING_SNAKE_CASE		)		)
     self.assertDictEqual(new_processor.tokenizer.get_vocab()   ,  processor.tokenizer.get_vocab()		)




   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[Any]:





    '''simple docstring'''




    CustomFeatureExtractor.register_for_auto_class()
    CustomTokenizer.register_for_auto_class()
    CustomProcessor.register_for_auto_class()

    UpperCAmelCase			:      List[str]			=			CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE		)

    with tempfile.TemporaryDirectory() as tmp_dir:
     UpperCAmelCase			:      Tuple			=			os.path.join(_SCREAMING_SNAKE_CASE   ,  """vocab.txt"""		)
     with open(_SCREAMING_SNAKE_CASE   ,  """w"""   ,  encoding="""utf-8"""		) as vocab_writer:
      vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]		)		)
     UpperCAmelCase			:      Any			=			CustomTokenizer(_SCREAMING_SNAKE_CASE		)

    UpperCAmelCase			:      List[Any]			=			CustomProcessor(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)

    with tempfile.TemporaryDirectory() as tmp_dir:
     create_repo(F"{USER}/test-dynamic-processor"   ,  token=self._token		)
     UpperCAmelCase			:      Optional[Any]			=			Repository(_SCREAMING_SNAKE_CASE   ,  clone_from=F"{USER}/test-dynamic-processor"   ,  token=self._token		)
     processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

     # This has added the proper auto_map field to the feature extractor config
     self.assertDictEqual(
         processor.feature_extractor.auto_map   ,  {
             """AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor""",
             """AutoProcessor""": """custom_processing.CustomProcessor""",
         }   ,  )

     # This has added the proper auto_map field to the tokenizer config
     with open(os.path.join(_SCREAMING_SNAKE_CASE   ,  """tokenizer_config.json"""		)		) as f:
      UpperCAmelCase			:      List[str]			=			json.load(_SCREAMING_SNAKE_CASE		)
     self.assertDictEqual(
         tokenizer_config["""auto_map"""]   ,  {
             """AutoTokenizer""": ["""custom_tokenization.CustomTokenizer""", None],
             """AutoProcessor""": """custom_processing.CustomProcessor""",
         }   ,  )

     # The code has been copied from fixtures
     self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE   ,  """custom_feature_extraction.py"""		)		)		)
     self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE   ,  """custom_tokenization.py"""		)		)		)
     self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE   ,  """custom_processing.py"""		)		)		)

     repo.push_to_hub()

    UpperCAmelCase			:      Optional[int]			=			AutoProcessor.from_pretrained(F"{USER}/test-dynamic-processor"   ,  trust_remote_code=_SCREAMING_SNAKE_CASE		)
    # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
    self.assertEqual(new_processor.__class__.__name__   ,  """CustomProcessor"""		)





"""simple docstring"""
import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict

@pytest.mark.parametrize(
    """files"""    ,	[
        ["""full:README.md""", """dataset_infos.json"""],
        ["""empty:README.md""", """dataset_infos.json"""],
        ["""dataset_infos.json"""],
        ["""full:README.md"""],
    ]    ,	)
def    _snake_case		(		UpperCamelCase		:	str    ,	UpperCamelCase		:	Optional[Any]		):
 UpperCAmelCase			:      List[Any]			=			tmp_path_factory.mktemp("""dset_infos_dir"""		)
 if "full:README.md" in files:
  with open(dataset_infos_dir / """README.md"""    ,	"""w"""		) as f:
   f.write("""---\ndataset_info:\n  dataset_size: 42\n---"""		)
 if "empty:README.md" in files:
  with open(dataset_infos_dir / """README.md"""    ,	"""w"""		) as f:
   f.write(""""""		)
    # we want to support dataset_infos.json for backward compatibility
 if "dataset_infos.json" in files:
  with open(dataset_infos_dir / """dataset_infos.json"""    ,	"""w"""		) as f:
   f.write("""{\"default\": {\"dataset_size\": 42}}"""		)
 UpperCAmelCase			:      str			=			DatasetInfosDict.from_directory(UpperCamelCase		)
 assert dataset_infos
 assert dataset_infos["default"].dataset_size == 42

@pytest.mark.parametrize(
    """dataset_info"""    ,	[
        DatasetInfo(),
        DatasetInfo(
            description="""foo"""    ,	features=Features({"""a""": Value("""int32"""		)}		)    ,	builder_name="""builder"""    ,	config_name="""config"""    ,	version="""1.0.0"""    ,	splits=[{"""name""": """train"""}]    ,	download_size=42    ,	),
    ]    ,	)
def    _snake_case		(		UpperCamelCase		:	str    ,	UpperCamelCase		:	DatasetInfo		):
 UpperCAmelCase			:      List[Any]			=			str(UpperCamelCase		)
 dataset_info.write_to_directory(UpperCamelCase		)
 UpperCAmelCase			:      List[str]			=			DatasetInfo.from_directory(UpperCamelCase		)
 assert dataset_info == reloaded
 assert os.path.exists(os.path.join(UpperCamelCase    ,	"""dataset_info.json"""		)		)

def    _snake_case		(		):
 UpperCAmelCase			:      List[str]			=			DatasetInfo(
     description="""foo"""    ,	citation="""bar"""    ,	homepage="""https://foo.bar"""    ,	license="""CC0"""    ,	features=Features({"""a""": Value("""int32"""		)}		)    ,	post_processed={}    ,	supervised_keys=()    ,	task_templates=[]    ,	builder_name="""builder"""    ,	config_name="""config"""    ,	version="""1.0.0"""    ,	splits=[{"""name""": """train""", """num_examples""": 42}]    ,	download_checksums={}    ,	download_size=1337    ,	post_processing_size=442    ,	dataset_size=1234    ,	size_in_bytes=1337 + 442 + 1234    ,	)
 UpperCAmelCase			:      str			=			dataset_info._to_yaml_dict()
 assert sorted(UpperCamelCase		) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML		)
 for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
  assert key in dataset_info_yaml_dict
  assert isinstance(dataset_info_yaml_dict[key]    ,	(list, dict, int, str)		)
 UpperCAmelCase			:      Union[str, Any]			=			yaml.safe_dump(UpperCamelCase		)
 UpperCAmelCase			:      List[str]			=			yaml.safe_load(UpperCamelCase		)
 assert dataset_info_yaml_dict == reloaded

def    _snake_case		(		):
 UpperCAmelCase			:      List[str]			=			DatasetInfo()
 UpperCAmelCase			:      Union[str, Any]			=			dataset_info._to_yaml_dict()
 assert dataset_info_yaml_dict == {}

@pytest.mark.parametrize(
    """dataset_infos_dict"""    ,	[
        DatasetInfosDict(),
        DatasetInfosDict({"""default""": DatasetInfo()}		),
        DatasetInfosDict({"""my_config_name""": DatasetInfo()}		),
        DatasetInfosDict(
            {
                """default""": DatasetInfo(
                    description="""foo"""    ,	features=Features({"""a""": Value("""int32"""		)}		)    ,	builder_name="""builder"""    ,	config_name="""config"""    ,	version="""1.0.0"""    ,	splits=[{"""name""": """train"""}]    ,	download_size=42    ,	)
            }		),
        DatasetInfosDict(
            {
                """v1""": DatasetInfo(dataset_size=42		),
                """v2""": DatasetInfo(dataset_size=1337		),
            }		),
    ]    ,	)
def    _snake_case		(		UpperCamelCase		:	Optional[int]    ,	UpperCamelCase		:	DatasetInfosDict		):
 UpperCAmelCase			:      List[str]			=			str(UpperCamelCase		)
 dataset_infos_dict.write_to_directory(UpperCamelCase		)
 UpperCAmelCase			:      List[Any]			=			DatasetInfosDict.from_directory(UpperCamelCase		)

 # the config_name of the dataset_infos_dict take over the attribute
 for config_name, dataset_info in dataset_infos_dict.items():
  UpperCAmelCase			:      Union[str, Any]			=			config_name
  # the yaml representation doesn't include fields like description or citation
  # so we just test that we can recover what we can from the yaml
  UpperCAmelCase			:      Optional[Any]			=			DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()		)
 assert dataset_infos_dict == reloaded

 if dataset_infos_dict:
  assert os.path.exists(os.path.join(UpperCamelCase    ,	"""README.md"""		)		)
