


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
lowerCamelCase						=		sys.version_info >= (3, 10)






def 					lowerCamelCase_   (	_a=None     ,       _a=None	):




		"""simple docstring"""






		return field(default_factory=lambda: default     ,       metadata=_a	)





@dataclass
class  _a							:
	_a  :  int
	_a  :  float
	_a  :  str
	_a  :  bool





@dataclass
class  _a							:
	_a  :  int				   =							42
	_a  :  str				   =							field(default='''toto'''					,						metadata={'''help''': '''help message'''})





@dataclass
class  _a							:
	_a  :  bool				   =							False
	_a  :  bool				   =							True
	_a  :  Optional[bool]				   =							None





class  _a							(    _lowercase):
	_a  :  List[Any]				   =							'''titi'''
	_a  :  int				   =							'''toto'''





class  _a							(    _lowercase):
	_a  :  List[Any]				   =							'''titi'''
	_a  :  Optional[Any]				   =							'''toto'''
	_a  :  int				   =							42





@dataclass
class  _a							:
	_a  :  BasicEnum				   =							"toto"




	def 							UpperCAmelCase__( self						:		List[str]		)->	List[Any]:
			lowerCAmelCase__       :      Tuple   					= BasicEnum(self.foo		)





@dataclass
class  _a							:
	_a  :  MixedTypeEnum				   =							"toto"




	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Dict:
			lowerCAmelCase__       :      Optional[Any]   					= MixedTypeEnum(self.foo		)





@dataclass
class  _a							:
	_a  :  Optional[int]				   =							None
	_a  :  Optional[float]				   =							field(default=_lowercase					,						metadata={'''help''': '''help message'''})
	_a  :  Optional[str]				   =							None
	_a  :  Optional[List[str]]				   =							list_field(default=[])
	_a  :  Optional[List[int]]				   =							list_field(default=[])





@dataclass
class  _a							:
	_a  :  List[int]				   =							list_field(default=[])
	_a  :  List[int]				   =							list_field(default=[1, 2, 3])
	_a  :  List[str]				   =							list_field(default=['''Hallo''', '''Bonjour''', '''Hello'''])
	_a  :  List[float]				   =							list_field(default=[0.1, 0.2, 0.3])





@dataclass
class  _a							:
	_a  :  List[int]				   =							field()
	_a  :  str				   =							field()
	_a  :  BasicEnum				   =							field()




	def 							UpperCAmelCase__( self						:		str		)->	Tuple:
			lowerCAmelCase__       :      Union[str, Any]   					= BasicEnum(self.required_enum		)





@dataclass
class  _a							:
	_a  :  int
	_a  :  "BasicEnum"				   =							field()
	_a  :  "Optional[bool]"				   =							None
	_a  :  "str"				   =							field(default='''toto'''					,						metadata={'''help''': '''help message'''})
	_a  :  "List[str]"				   =							list_field(default=['''Hallo''', '''Bonjour''', '''Hello'''])


if is_python_no_less_than_3_10:





		@dataclass
		class  _a							:
			_a  :  bool				   =							False
			_a  :  bool				   =							True
			_a  :  bool | None				   =							None





		@dataclass
		class  _a							:
			_a  :  int | None				   =							None
			_a  :  float | None				   =							field(default=_lowercase					,						metadata={'''help''': '''help message'''})
			_a  :  str | None				   =							None
			_a  :  list[str] | None				   =							list_field(default=[])
			_a  :  list[int] | None				   =							list_field(default=[])





class  _a							(    unittest.TestCase):




	def 							UpperCAmelCase__( self						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		argparse.ArgumentParser		, _SCREAMING_SNAKE_CASE						:		argparse.ArgumentParser		)->	List[str]:
			self.assertEqual(len(a._actions		)		, len(b._actions		)		)
			for x, y in zip(a._actions		, b._actions		):
					lowerCAmelCase__       :      Tuple   					= {k: v for k, v in vars(_SCREAMING_SNAKE_CASE		).items() if k != '''container'''}
					lowerCAmelCase__       :      str   					= {k: v for k, v in vars(_SCREAMING_SNAKE_CASE		).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'''		, _SCREAMING_SNAKE_CASE		) and yy.get('''choices'''		, _SCREAMING_SNAKE_CASE		):
							for expected_choice in yy["choices"] + xx["choices"]:
									self.assertEqual(xx['''type'''](_SCREAMING_SNAKE_CASE		)		, yy['''type'''](_SCREAMING_SNAKE_CASE		)		)
							del xx["type"], yy["type"]

					self.assertEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		List[str]		)->	str:
			lowerCAmelCase__       :      str   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Any   					= argparse.ArgumentParser()
			expected.add_argument('''--foo'''		, type=_SCREAMING_SNAKE_CASE		, required=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--bar'''		, type=_SCREAMING_SNAKE_CASE		, required=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--baz'''		, type=_SCREAMING_SNAKE_CASE		, required=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--flag'''		, type=_SCREAMING_SNAKE_CASE		, default=_SCREAMING_SNAKE_CASE		, const=_SCREAMING_SNAKE_CASE		, nargs='''?'''		)
			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Tuple   					= ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5''']
			((lowerCAmelCase__)					,     )       :      Dict   					= parser.parse_args_into_dataclasses(_SCREAMING_SNAKE_CASE		, look_for_args_file=_SCREAMING_SNAKE_CASE		)
			self.assertFalse(example.flag		)




	def 							UpperCAmelCase__( self						:		str		)->	Tuple:
			lowerCAmelCase__       :      List[str]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Optional[int]   					= argparse.ArgumentParser()
			expected.add_argument('''--foo'''		, default=42		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--baz'''		, default='''toto'''		, type=_SCREAMING_SNAKE_CASE		, help='''help message'''		)
			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		str		)->	Dict:
			lowerCAmelCase__       :      List[Any]   					= argparse.ArgumentParser()
			expected.add_argument('''--foo'''		, type=_SCREAMING_SNAKE_CASE		, default=_SCREAMING_SNAKE_CASE		, const=_SCREAMING_SNAKE_CASE		, nargs='''?'''		)
			expected.add_argument('''--baz'''		, type=_SCREAMING_SNAKE_CASE		, default=_SCREAMING_SNAKE_CASE		, const=_SCREAMING_SNAKE_CASE		, 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=_SCREAMING_SNAKE_CASE		, dest='''baz'''		)
			expected.add_argument('''--opt'''		, type=_SCREAMING_SNAKE_CASE		, default=_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Optional[Any]   					= [WithDefaultBoolExample]
			if is_python_no_less_than_3_10:
					dataclass_types.append(_SCREAMING_SNAKE_CASE		)

			for dataclass_type in dataclass_types:
					lowerCAmelCase__       :      Any   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)
					self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

					lowerCAmelCase__       :      Dict   					= parser.parse_args([]		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, opt=_SCREAMING_SNAKE_CASE		)		)

					lowerCAmelCase__       :      str   					= parser.parse_args(['''--foo''', '''--no_baz''']		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, opt=_SCREAMING_SNAKE_CASE		)		)

					lowerCAmelCase__       :      List[Any]   					= parser.parse_args(['''--foo''', '''--baz''']		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, opt=_SCREAMING_SNAKE_CASE		)		)

					lowerCAmelCase__       :      Union[str, Any]   					= parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True''']		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, opt=_SCREAMING_SNAKE_CASE		)		)

					lowerCAmelCase__       :      int   					= parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False''']		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, opt=_SCREAMING_SNAKE_CASE		)		)




	def 							UpperCAmelCase__( self						:		Tuple		)->	str:
			lowerCAmelCase__       :      Dict   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Optional[Any]   					= argparse.ArgumentParser()
			expected.add_argument(
			    '''--foo'''		, default='''toto'''		, choices=['''titi''', '''toto''', 42]		, type=make_choice_type_function(['''titi''', '''toto''', 42]		)		, )
			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Tuple   					= parser.parse_args([]		)
			self.assertEqual(args.foo		, '''toto'''		)
			lowerCAmelCase__       :      Optional[int]   					= parser.parse_args_into_dataclasses([]		)[0]
			self.assertEqual(enum_ex.foo		, MixedTypeEnum.toto		)

			lowerCAmelCase__       :      List[str]   					= parser.parse_args(['''--foo''', '''titi''']		)
			self.assertEqual(args.foo		, '''titi'''		)
			lowerCAmelCase__       :      Any   					= parser.parse_args_into_dataclasses(['''--foo''', '''titi''']		)[0]
			self.assertEqual(enum_ex.foo		, MixedTypeEnum.titi		)

			lowerCAmelCase__       :      str   					= parser.parse_args(['''--foo''', '''42''']		)
			self.assertEqual(args.foo		, 42		)
			lowerCAmelCase__       :      Dict   					= parser.parse_args_into_dataclasses(['''--foo''', '''42''']		)[0]
			self.assertEqual(enum_ex.foo		, MixedTypeEnum.fourtytwo		)




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





			@dataclass
			class  _a							:
				_a  :  Literal["titi", "toto", 42]				   =							"toto"
			lowerCAmelCase__       :      Optional[int]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[str]   					= argparse.ArgumentParser()
			expected.add_argument(
			    '''--foo'''		, default='''toto'''		, choices=('''titi''', '''toto''', 42)		, type=make_choice_type_function(['''titi''', '''toto''', 42]		)		, )
			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Any   					= parser.parse_args([]		)
			self.assertEqual(args.foo		, '''toto'''		)

			lowerCAmelCase__       :      List[Any]   					= parser.parse_args(['''--foo''', '''titi''']		)
			self.assertEqual(args.foo		, '''titi'''		)

			lowerCAmelCase__       :      Optional[Any]   					= parser.parse_args(['''--foo''', '''42''']		)
			self.assertEqual(args.foo		, 42		)




	def 							UpperCAmelCase__( self						:		Dict		)->	Tuple:
			lowerCAmelCase__       :      Optional[Any]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[Any]   					= argparse.ArgumentParser()
			expected.add_argument('''--foo_int'''		, nargs='''+'''		, default=[]		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--bar_int'''		, nargs='''+'''		, default=[1, 2, 3]		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--foo_str'''		, nargs='''+'''		, default=['''Hallo''', '''Bonjour''', '''Hello''']		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--foo_float'''		, nargs='''+'''		, default=[0.1, 0.2, 0.3]		, type=_SCREAMING_SNAKE_CASE		)

			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[Any]   					= parser.parse_args([]		)
			self.assertEqual(
			    _SCREAMING_SNAKE_CASE		, Namespace(foo_int=[]		, bar_int=[1, 2, 3]		, foo_str=['''Hallo''', '''Bonjour''', '''Hello''']		, foo_float=[0.1, 0.2, 0.3]		)		, )

			lowerCAmelCase__       :      int   					= parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split()		)
			self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo_int=[1]		, bar_int=[2, 3]		, foo_str=['''a''', '''b''', '''c''']		, foo_float=[0.1, 0.7]		)		)




	def 							UpperCAmelCase__( self						:		str		)->	int:
			lowerCAmelCase__       :      Any   					= argparse.ArgumentParser()
			expected.add_argument('''--foo'''		, default=_SCREAMING_SNAKE_CASE		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--bar'''		, default=_SCREAMING_SNAKE_CASE		, type=_SCREAMING_SNAKE_CASE		, help='''help message'''		)
			expected.add_argument('''--baz'''		, default=_SCREAMING_SNAKE_CASE		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--ces'''		, nargs='''+'''		, default=[]		, type=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--des'''		, nargs='''+'''		, default=[]		, type=_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[Any]   					= [OptionalExample]
			if is_python_no_less_than_3_10:
					dataclass_types.append(_SCREAMING_SNAKE_CASE		)

			for dataclass_type in dataclass_types:
					lowerCAmelCase__       :      Optional[int]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

					self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)

					lowerCAmelCase__       :      List[Any]   					= parser.parse_args([]		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=_SCREAMING_SNAKE_CASE		, bar=_SCREAMING_SNAKE_CASE		, baz=_SCREAMING_SNAKE_CASE		, ces=[]		, des=[]		)		)

					lowerCAmelCase__       :      Optional[Any]   					= parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split()		)
					self.assertEqual(_SCREAMING_SNAKE_CASE		, Namespace(foo=12		, bar=3.14		, baz='''42'''		, ces=['''a''', '''b''', '''c''']		, des=[1, 2, 3]		)		)




	def 							UpperCAmelCase__( self						:		Optional[int]		)->	List[Any]:
			lowerCAmelCase__       :      Union[str, Any]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[Any]   					= argparse.ArgumentParser()
			expected.add_argument('''--required_list'''		, nargs='''+'''		, type=_SCREAMING_SNAKE_CASE		, required=_SCREAMING_SNAKE_CASE		)
			expected.add_argument('''--required_str'''		, type=_SCREAMING_SNAKE_CASE		, required=_SCREAMING_SNAKE_CASE		)
			expected.add_argument(
			    '''--required_enum'''		, type=make_choice_type_function(['''titi''', '''toto''']		)		, choices=['''titi''', '''toto''']		, required=_SCREAMING_SNAKE_CASE		, )
			self.argparsersEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		List[Any]		)->	str:
			lowerCAmelCase__       :      Dict   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

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




	def 							UpperCAmelCase__( self						:		Any		)->	Dict:
			lowerCAmelCase__       :      int   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Any   					= {
			    '''foo''': 12,
			    '''bar''': 3.14,
			    '''baz''': '''42''',
			    '''flag''': True,
			}

			lowerCAmelCase__       :      Dict   					= parser.parse_dict(_SCREAMING_SNAKE_CASE		)[0]
			lowerCAmelCase__       :      Optional[int]   					= BasicExample(**_SCREAMING_SNAKE_CASE		)
			self.assertEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		str		)->	Union[str, Any]:
			lowerCAmelCase__       :      Dict   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Dict   					= {
			    '''foo''': 12,
			    '''bar''': 3.14,
			    '''baz''': '''42''',
			    '''flag''': True,
			    '''extra''': 42,
			}

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




	def 							UpperCAmelCase__( self						:		Dict		)->	List[str]:
			lowerCAmelCase__       :      Dict   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[str]   					= {
			    '''foo''': 12,
			    '''bar''': 3.14,
			    '''baz''': '''42''',
			    '''flag''': True,
			}
			with tempfile.TemporaryDirectory() as tmp_dir:
					lowerCAmelCase__       :      Optional[int]   					= os.path.join(_SCREAMING_SNAKE_CASE		, '''temp_json'''		)
					os.mkdir(_SCREAMING_SNAKE_CASE		)
					with open(temp_local_path + '''.json'''		, '''w+'''		) as f:
							json.dump(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      str   					= parser.parse_yaml_file(Path(temp_local_path + '''.json'''		)		)[0]

			lowerCAmelCase__       :      Optional[int]   					= BasicExample(**_SCREAMING_SNAKE_CASE		)
			self.assertEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Any		)->	List[str]:
			lowerCAmelCase__       :      Dict   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[Any]   					= {
			    '''foo''': 12,
			    '''bar''': 3.14,
			    '''baz''': '''42''',
			    '''flag''': True,
			}
			with tempfile.TemporaryDirectory() as tmp_dir:
					lowerCAmelCase__       :      Optional[int]   					= os.path.join(_SCREAMING_SNAKE_CASE		, '''temp_yaml'''		)
					os.mkdir(_SCREAMING_SNAKE_CASE		)
					with open(temp_local_path + '''.yaml'''		, '''w+'''		) as f:
							yaml.dump(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      List[str]   					= parser.parse_yaml_file(Path(temp_local_path + '''.yaml'''		)		)[0]
			lowerCAmelCase__       :      Union[str, Any]   					= BasicExample(**_SCREAMING_SNAKE_CASE		)
			self.assertEqual(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		int		)->	Any:
			lowerCAmelCase__       :      List[str]   					= HfArgumentParser(_SCREAMING_SNAKE_CASE		)
			self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)








def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		if number < 0:
				raise ValueError('''number must not be negative'''	)
		return number & (number - 1) == 0


if __name__ == "__main__":
		import doctest

		doctest.testmod()




