





import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
UpperCAmelCase__						:			Optional[Any]									=sys.version_info >= (3, 10)





def      _lowercase					(						_UpperCAmelCase=None			,      _UpperCAmelCase=None				)						->  Dict:
				return field(default_factory=lambda: default			,      metadata=_UpperCAmelCase				)

@dataclass
class       __A     :
				__A      =				42
				__A      =				42
				__A      =				42
				__A      =				42




@dataclass
class       __A     :
				__A      =				42
				__A      =				field(default="""toto"""    ,					metadata={"""help""": """help message"""}    )




@dataclass
class       __A     :
				__A      =				False
				__A      =				True
				__A      =				None




class       __A     (						a    ):
				__A      =				"""titi"""
				__A      =				"""toto"""




class       __A     (						a    ):
				__A      =				"""titi"""
				__A      =				"""toto"""
				__A      =				42




@dataclass
class       __A     :
				__A      =				"toto"


				def _snake_case       (		self							):
								lowerCamelCase	 =BasicEnum(self.foo							)




@dataclass
class       __A     :
				__A      =				"toto"


				def _snake_case       (		self							):
								lowerCamelCase	 =MixedTypeEnum(self.foo							)




@dataclass
class       __A     :
				__A      =				None
				__A      =				field(default=a    ,					metadata={"""help""": """help message"""}    )
				__A      =				None
				__A      =				list_field(default=[]    )
				__A      =				list_field(default=[]    )




@dataclass
class       __A     :
				__A      =				list_field(default=[]    )
				__A      =				list_field(default=[1, 2, 3]    )
				__A      =				list_field(default=["""Hallo""", """Bonjour""", """Hello"""]    )
				__A      =				list_field(default=[0.1, 0.2, 0.3]    )




@dataclass
class       __A     :
				__A      =				field()
				__A      =				field()
				__A      =				field()


				def _snake_case       (		self							):
								lowerCamelCase	 =BasicEnum(self.required_enum							)




@dataclass
class       __A     :
				__A      =				42
				__A      =				field()
				__A      =				None
				__A      =				field(default="""toto"""    ,					metadata={"""help""": """help message"""}    )
				__A      =				list_field(default=["""Hallo""", """Bonjour""", """Hello"""]    )


if is_python_no_less_than_3_10:

							@dataclass
							class       __A     :
											__A      =				False
											__A      =				True
											__A      =				None




							@dataclass
							class       __A     :
											__A      =				None
											__A      =				field(default=a    ,					metadata={"""help""": """help message"""}    )
											__A      =				None
											__A      =				list_field(default=[]    )
											__A      =				list_field(default=[]    )




class       __A     (						unittest.TestCase    ):


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_							):
								self.assertEqual(len(a._actions							)   , len(b._actions							)							)
								for x, y in zip(a._actions   , b._actions							):
												lowerCamelCase	 ={k: v for k, v in vars(UpperCAmelCase_							).items() if k != """container"""}
												lowerCamelCase	 ={k: v for k, v in vars(UpperCAmelCase_							).items() if k != """container"""}

												# Choices with mixed type have custom function as "type"
												# So we need to compare results directly for equality
												if xx.get("""choices"""   , UpperCAmelCase_							) and yy.get("""choices"""   , UpperCAmelCase_							):
																for expected_choice in yy["choices"] + xx["choices"]:
																				self.assertEqual(xx["""type"""](UpperCAmelCase_							)   , yy["""type"""](UpperCAmelCase_							)							)
																del xx["type"], yy["type"]

												self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument("""--bar"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument("""--baz"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument("""--flag"""   , type=UpperCAmelCase_   , default=UpperCAmelCase_   , const=UpperCAmelCase_   , nargs="""?"""							)
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								lowerCamelCase	 =["""--foo""", """1""", """--baz""", """quux""", """--bar""", """0.5"""]
								((lowerCamelCase) ,  )	 =parser.parse_args_into_dataclasses(UpperCAmelCase_   , look_for_args_file=UpperCAmelCase_							)
								self.assertFalse(example.flag							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo"""   , default=42   , type=UpperCAmelCase_							)
								expected.add_argument("""--baz"""   , default="""toto"""   , type=UpperCAmelCase_   , help="""help message"""							)
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo"""   , type=UpperCAmelCase_   , default=UpperCAmelCase_   , const=UpperCAmelCase_   , nargs="""?"""							)
								expected.add_argument("""--baz"""   , type=UpperCAmelCase_   , default=UpperCAmelCase_   , const=UpperCAmelCase_   , nargs="""?"""							)
								# A boolean no_* argument always has to come after its "default: True" regular counter-part
								# and its default must be set to False
								expected.add_argument("""--no_baz"""   , action="""store_false"""   , default=UpperCAmelCase_   , dest="""baz"""							)
								expected.add_argument("""--opt"""   , type=UpperCAmelCase_   , default=UpperCAmelCase_							)

								lowerCamelCase	 =[WithDefaultBoolExample]
								if is_python_no_less_than_3_10:
												dataclass_types.append(UpperCAmelCase_							)

								for dataclass_type in dataclass_types:
												lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)
												self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

												lowerCamelCase	 =parser.parse_args([]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , baz=UpperCAmelCase_   , opt=UpperCAmelCase_							)							)

												lowerCamelCase	 =parser.parse_args(["""--foo""", """--no_baz"""]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , baz=UpperCAmelCase_   , opt=UpperCAmelCase_							)							)

												lowerCamelCase	 =parser.parse_args(["""--foo""", """--baz"""]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , baz=UpperCAmelCase_   , opt=UpperCAmelCase_							)							)

												lowerCamelCase	 =parser.parse_args(["""--foo""", """True""", """--baz""", """True""", """--opt""", """True"""]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , baz=UpperCAmelCase_   , opt=UpperCAmelCase_							)							)

												lowerCamelCase	 =parser.parse_args(["""--foo""", """False""", """--baz""", """False""", """--opt""", """False"""]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , baz=UpperCAmelCase_   , opt=UpperCAmelCase_							)							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument(
								    """--foo"""   , default="""toto"""   , choices=["""titi""", """toto""", 42]   , type=make_choice_type_function(["""titi""", """toto""", 42]							)   , )
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								lowerCamelCase	 =parser.parse_args([]							)
								self.assertEqual(args.foo   , """toto"""							)
								lowerCamelCase	 =parser.parse_args_into_dataclasses([]							)[0]
								self.assertEqual(enum_ex.foo   , MixedTypeEnum.toto							)

								lowerCamelCase	 =parser.parse_args(["""--foo""", """titi"""]							)
								self.assertEqual(args.foo   , """titi"""							)
								lowerCamelCase	 =parser.parse_args_into_dataclasses(["""--foo""", """titi"""]							)[0]
								self.assertEqual(enum_ex.foo   , MixedTypeEnum.titi							)

								lowerCamelCase	 =parser.parse_args(["""--foo""", """42"""]							)
								self.assertEqual(args.foo   , 42							)
								lowerCamelCase	 =parser.parse_args_into_dataclasses(["""--foo""", """42"""]							)[0]
								self.assertEqual(enum_ex.foo   , MixedTypeEnum.fourtytwo							)


				def _snake_case       (		self							):

								@dataclass
								class       __A     :
												__A      =				"toto"




								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument(
								    """--foo"""   , default="""toto"""   , choices=("""titi""", """toto""", 42)   , type=make_choice_type_function(["""titi""", """toto""", 42]							)   , )
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								lowerCamelCase	 =parser.parse_args([]							)
								self.assertEqual(args.foo   , """toto"""							)

								lowerCamelCase	 =parser.parse_args(["""--foo""", """titi"""]							)
								self.assertEqual(args.foo   , """titi"""							)

								lowerCamelCase	 =parser.parse_args(["""--foo""", """42"""]							)
								self.assertEqual(args.foo   , 42							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo_int"""   , nargs="""+"""   , default=[]   , type=UpperCAmelCase_							)
								expected.add_argument("""--bar_int"""   , nargs="""+"""   , default=[1, 2, 3]   , type=UpperCAmelCase_							)
								expected.add_argument("""--foo_str"""   , nargs="""+"""   , default=["""Hallo""", """Bonjour""", """Hello"""]   , type=UpperCAmelCase_							)
								expected.add_argument("""--foo_float"""   , nargs="""+"""   , default=[0.1, 0.2, 0.3]   , type=UpperCAmelCase_							)

								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

								lowerCamelCase	 =parser.parse_args([]							)
								self.assertEqual(
								    UpperCAmelCase_   , Namespace(foo_int=[]   , bar_int=[1, 2, 3]   , foo_str=["""Hallo""", """Bonjour""", """Hello"""]   , foo_float=[0.1, 0.2, 0.3]							)   , )

								lowerCamelCase	 =parser.parse_args("""--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7""".split()							)
								self.assertEqual(UpperCAmelCase_   , Namespace(foo_int=[1]   , bar_int=[2, 3]   , foo_str=["""a""", """b""", """c"""]   , foo_float=[0.1, 0.7]							)							)


				def _snake_case       (		self							):
								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo"""   , default=UpperCAmelCase_   , type=UpperCAmelCase_							)
								expected.add_argument("""--bar"""   , default=UpperCAmelCase_   , type=UpperCAmelCase_   , help="""help message"""							)
								expected.add_argument("""--baz"""   , default=UpperCAmelCase_   , type=UpperCAmelCase_							)
								expected.add_argument("""--ces"""   , nargs="""+"""   , default=[]   , type=UpperCAmelCase_							)
								expected.add_argument("""--des"""   , nargs="""+"""   , default=[]   , type=UpperCAmelCase_							)

								lowerCamelCase	 =[OptionalExample]
								if is_python_no_less_than_3_10:
												dataclass_types.append(UpperCAmelCase_							)

								for dataclass_type in dataclass_types:
												lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

												self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)

												lowerCamelCase	 =parser.parse_args([]							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=UpperCAmelCase_   , bar=UpperCAmelCase_   , baz=UpperCAmelCase_   , ces=[]   , des=[]							)							)

												lowerCamelCase	 =parser.parse_args("""--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3""".split()							)
												self.assertEqual(UpperCAmelCase_   , Namespace(foo=12   , bar=3.1_4   , baz="""42"""   , ces=["""a""", """b""", """c"""]   , des=[1, 2, 3]							)							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--required_list"""   , nargs="""+"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument("""--required_str"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument(
								    """--required_enum"""   , type=make_choice_type_function(["""titi""", """toto"""]							)   , choices=["""titi""", """toto"""]   , required=UpperCAmelCase_   , )
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 =argparse.ArgumentParser()
								expected.add_argument("""--foo"""   , type=UpperCAmelCase_   , required=UpperCAmelCase_							)
								expected.add_argument(
								    """--required_enum"""   , type=make_choice_type_function(["""titi""", """toto"""]							)   , choices=["""titi""", """toto"""]   , required=UpperCAmelCase_   , )
								expected.add_argument("""--opt"""   , type=UpperCAmelCase_   , default=UpperCAmelCase_							)
								expected.add_argument("""--baz"""   , default="""toto"""   , type=UpperCAmelCase_   , help="""help message"""							)
								expected.add_argument("""--foo_str"""   , nargs="""+"""   , default=["""Hallo""", """Bonjour""", """Hello"""]   , type=UpperCAmelCase_							)
								self.argparsersEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 ={
								    """foo""": 12,
								    """bar""": 3.1_4,
								    """baz""": """42""",
								    """flag""": True,
								}

								lowerCamelCase	 =parser.parse_dict(UpperCAmelCase_							)[0]
								lowerCamelCase	 =BasicExample(**UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 ={
								    """foo""": 12,
								    """bar""": 3.1_4,
								    """baz""": """42""",
								    """flag""": True,
								    """extra""": 42,
								}

								self.assertRaises(UpperCAmelCase_   , parser.parse_dict   , UpperCAmelCase_   , allow_extra_keys=UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 ={
								    """foo""": 12,
								    """bar""": 3.1_4,
								    """baz""": """42""",
								    """flag""": True,
								}
								with tempfile.TemporaryDirectory() as tmp_dir:
												lowerCamelCase	 =os.path.join(UpperCAmelCase_   , """temp_json"""							)
												os.mkdir(UpperCAmelCase_							)
												with open(temp_local_path + """.json"""   , """w+"""							) as f:
																json.dump(UpperCAmelCase_   , UpperCAmelCase_							)
												lowerCamelCase	 =parser.parse_yaml_file(Path(temp_local_path + """.json"""							)							)[0]

								lowerCamelCase	 =BasicExample(**UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)

								lowerCamelCase	 ={
								    """foo""": 12,
								    """bar""": 3.1_4,
								    """baz""": """42""",
								    """flag""": True,
								}
								with tempfile.TemporaryDirectory() as tmp_dir:
												lowerCamelCase	 =os.path.join(UpperCAmelCase_   , """temp_yaml"""							)
												os.mkdir(UpperCAmelCase_							)
												with open(temp_local_path + """.yaml"""   , """w+"""							) as f:
																yaml.dump(UpperCAmelCase_   , UpperCAmelCase_							)
												lowerCamelCase	 =parser.parse_yaml_file(Path(temp_local_path + """.yaml"""							)							)[0]
								lowerCamelCase	 =BasicExample(**UpperCAmelCase_							)
								self.assertEqual(UpperCAmelCase_   , UpperCAmelCase_							)


				def _snake_case       (		self							):
								lowerCamelCase	 =HfArgumentParser(UpperCAmelCase_							)
								self.assertIsNotNone(UpperCAmelCase_							)












from abc import ABC, abstractmethod
from argparse import ArgumentParser

class       __A     (						a    ):


				@staticmethod
				@abstractmethod
				def _snake_case       (		UpperCAmelCase_							):
								raise NotImplementedError()


				@abstractmethod
				def _snake_case       (		self							):
								raise NotImplementedError()





