






from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching






def   UpperCamelCase  (       ):




     '''simple docstring'''






     import os as original_os
     from os import path as original_path
     from os import rename as original_rename
     from os.path import dirname as original_dirname
     from os.path import join as original_join

     assert _test_patching.os is original_os
     assert _test_patching.path is original_path
     assert _test_patching.join is original_join

     assert _test_patching.renamed_os is original_os
     assert _test_patching.renamed_path is original_path
     assert _test_patching.renamed_join is original_join

     __snake_case			:Union[str, Any]          =						"""__test_patch_submodule_mock__"""
     with patch_submodule(_test_patching	,"""os.path.join"""	,snake_case__  ):
          # Every way to access os.path.join must be patched, and the rest must stay untouched

          # check os.path.join
          assert isinstance(_test_patching.os	,_PatchedModuleObj  )
          assert isinstance(_test_patching.os.path	,_PatchedModuleObj  )
          assert _test_patching.os.path.join is mock

          # check path.join
          assert isinstance(_test_patching.path	,_PatchedModuleObj  )
          assert _test_patching.path.join is mock

          # check join
          assert _test_patching.join is mock

          # check that the other attributes are untouched
          assert _test_patching.os.rename is original_rename
          assert _test_patching.path.dirname is original_dirname
          assert _test_patching.os.path.dirname is original_dirname

          # Even renamed modules or objects must be patched

          # check renamed_os.path.join
          assert isinstance(_test_patching.renamed_os	,_PatchedModuleObj  )
          assert isinstance(_test_patching.renamed_os.path	,_PatchedModuleObj  )
          assert _test_patching.renamed_os.path.join is mock

          # check renamed_path.join
          assert isinstance(_test_patching.renamed_path	,_PatchedModuleObj  )
          assert _test_patching.renamed_path.join is mock

          # check renamed_join
          assert _test_patching.renamed_join is mock

          # check that the other attributes are untouched
          assert _test_patching.renamed_os.rename is original_rename
          assert _test_patching.renamed_path.dirname is original_dirname
          assert _test_patching.renamed_os.path.dirname is original_dirname

     # check that everthing is back to normal when the patch is over

     assert _test_patching.os is original_os
     assert _test_patching.path is original_path
     assert _test_patching.join is original_join

     assert _test_patching.renamed_os is original_os
     assert _test_patching.renamed_path is original_path
     assert _test_patching.renamed_join is original_join






def   UpperCamelCase  (       ):




     '''simple docstring'''






     assert _test_patching.open is open

     __snake_case			:List[Any]          =						"""__test_patch_submodule_builtin_mock__"""
     # _test_patching has "open" in its globals
     assert _test_patching.open is open
     with patch_submodule(_test_patching	,"""open"""	,snake_case__  ):
          assert _test_patching.open is mock

     # check that everthing is back to normal when the patch is over

     assert _test_patching.open is open






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:Optional[Any]          =						"""__test_patch_submodule_missing_mock__"""
     with patch_submodule(_test_patching	,"""pandas.read_csv"""	,snake_case__  ):
          pass






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:Optional[Any]          =						"""__test_patch_submodule_missing_builtin_mock__"""
     # _test_patching doesn't have "len" in its globals
     assert getattr(_test_patching	,"""len"""	,snake_case__  ) is None
     with patch_submodule(_test_patching	,"""len"""	,snake_case__  ):
          assert _test_patching.len is mock
     assert _test_patching.len is len






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:Tuple          =						"""__test_patch_submodule_start_and_stop_mock__"""
     __snake_case			:Any          =						patch_submodule(_test_patching	,"""open"""	,snake_case__  )
     assert _test_patching.open is open
     patch.start()
     assert _test_patching.open is mock
     patch.stop()
     assert _test_patching.open is open






def   UpperCamelCase  (       ):




     '''simple docstring'''






     from os import rename as original_rename
     from os.path import dirname as original_dirname
     from os.path import join as original_join

     __snake_case			:List[Any]          =						"""__test_patch_submodule_successive_join__"""
     __snake_case			:Any          =						"""__test_patch_submodule_successive_dirname__"""
     __snake_case			:Optional[int]          =						"""__test_patch_submodule_successive_rename__"""
     assert _test_patching.os.path.join is original_join
     assert _test_patching.os.path.dirname is original_dirname
     assert _test_patching.os.rename is original_rename

     with patch_submodule(_test_patching	,"""os.path.join"""	,snake_case__  ):
          with patch_submodule(_test_patching	,"""os.rename"""	,snake_case__  ):
               with patch_submodule(_test_patching	,"""os.path.dirname"""	,snake_case__  ):
                    assert _test_patching.os.path.join is mock_join
                    assert _test_patching.os.path.dirname is mock_dirname
                    assert _test_patching.os.rename is mock_rename

    # try another order
     with patch_submodule(_test_patching	,"""os.rename"""	,snake_case__  ):
          with patch_submodule(_test_patching	,"""os.path.join"""	,snake_case__  ):
               with patch_submodule(_test_patching	,"""os.path.dirname"""	,snake_case__  ):
                    assert _test_patching.os.path.join is mock_join
                    assert _test_patching.os.path.dirname is mock_dirname
                    assert _test_patching.os.rename is mock_rename

     assert _test_patching.os.path.join is original_join
     assert _test_patching.os.path.dirname is original_dirname
     assert _test_patching.os.rename is original_rename






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:int          =						"""__test_patch_submodule_doesnt_exist_mock__"""
     with patch_submodule(_test_patching	,"""__module_that_doesn_exist__.__attribute_that_doesn_exist__"""	,snake_case__  ):
          pass
     with patch_submodule(_test_patching	,"""os.__attribute_that_doesn_exist__"""	,snake_case__  ):
          pass










import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCamelCase__							=     get_tests_dir("""fixtures/dummy-config.json""")




class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



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






            '''simple docstring'''





            __snake_case			:Optional[int]          =						0



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            self.assertIsNotNone(transformers.models.auto.__spec__    )
            self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""    )    )



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






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained("""bert-base-uncased"""    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            __snake_case			:List[str]          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						AutoConfig.for_model("""roberta"""    )
            self.assertIsInstance(a__			,						a__    )



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






            '''simple docstring'''





            with tempfile.TemporaryDirectory() as tmp_dir:
                 # This model name contains bert and roberta, but roberta ends up being picked.
                 __snake_case			:List[Any]          =						os.path.join(a__			,						"""fake-roberta"""    )
                 os.makedirs(a__			,						exist_ok=a__    )
                 with open(os.path.join(a__			,						"""config.json"""    )			,						"""w"""    ) as f:
                      f.write(json.dumps({}    )    )
                 __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained(a__    )
                 self.assertEqual(type(a__    )			,						a__    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            try:
                 AutoConfig.register("""custom"""			,						a__    )
                 # Wrong model type will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""model"""			,						a__    )
                 # Trying to register something existing in the Transformers library will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""bert"""			,						a__    )

                 # Now that the config is registered, it can be used as any other config with the auto-API
                 __snake_case			:Optional[int]          =						CustomConfig()
                 with tempfile.TemporaryDirectory() as tmp_dir:
                      config.save_pretrained(a__    )
                      __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
                      self.assertIsInstance(a__			,						a__    )

            finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["custom"]



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""bert-base is not a local folder and is not a valid model identifier"""    ):
                 __snake_case			:str          =						AutoConfig.from_pretrained("""bert-base"""    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained(a__			,						revision="""aaaaaa"""    )



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






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""			,						):
                 __snake_case			:Optional[int]          =						AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""    )



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaises(a__    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
            # If remote code is disabled, we can't load this config.
            with self.assertRaises(a__    ):
                 __snake_case			:int          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )

            __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
            self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            # Test config can be reloaded.
            with tempfile.TemporaryDirectory() as tmp_dir:
                 config.save_pretrained(a__    )
                 __snake_case			:Union[str, Any]          =						AutoConfig.from_pretrained(a__			,						trust_remote_code=a__    )
            self.assertEqual(reloaded_config.__class__.__name__			,						"""NewModelConfig"""    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            class 			snake_case__   (							lowercase_):

                   '''simple docstring'''
                   lowerCamelCase							:				Optional[Any] 				= "new-model"

            try:
                 AutoConfig.register("""new-model"""			,						a__    )
                 # If remote code is not set, the default is to use local
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote code is disabled, we load the local one.
                 __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote is enabled, we load from the Hub
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            finally:
                 if "new-model" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["new-model"]


