





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	:					Optional[int]			  =					sys.version_info >= (3, 10)



def 		A_    (					_lowerCAmelCase=None							,       _lowerCAmelCase=None					)						->			Tuple:
 return field(default_factory=lambda: default							,       metadata=_lowerCAmelCase					)



@dataclass
class  A__       :
      _UpperCAmelCase     :int
      _UpperCAmelCase     :float
      _UpperCAmelCase     :str
      _UpperCAmelCase     :bool



@dataclass
class  A__       :
      _UpperCAmelCase     :int						   =       4_2
      _UpperCAmelCase     :str						   =       field(default='toto'	,       metadata={'help': 'help message'}      )



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



class  A__       (	__snake_case      ):
      _UpperCAmelCase     :int						   =       'titi'
      _UpperCAmelCase     :int						   =       'toto'



class  A__       (	__snake_case      ):
      _UpperCAmelCase     :Tuple						   =       'titi'
      _UpperCAmelCase     :List[str]						   =       'toto'
      _UpperCAmelCase     :Tuple						   =       4_2



@dataclass
class  A__       :
      _UpperCAmelCase     :BasicEnum						   =       "toto"





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       BasicEnum(self.foo       )



@dataclass
class  A__       :
      _UpperCAmelCase     :MixedTypeEnum						   =       "toto"





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       MixedTypeEnum(self.foo       )



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



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



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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[Any]	     =       BasicEnum(self.required_enum       )



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


if is_python_no_less_than_3_10:



    @dataclass
    class  A__       :
          _UpperCAmelCase     :bool						   =       False
          _UpperCAmelCase     :bool						   =       True
          _UpperCAmelCase     :bool | None						   =       None



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



class  A__       (	unittest.TestCase      ):





      def 	__UpperCamelCase(			self	,							A_	,							A_       ):
       '''simple docstring'''




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

        self.assertEqual(A_	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       HfArgumentParser(A_       )

       UpperCamelCase     : int	     =       argparse.ArgumentParser()
       expected.add_argument("--foo"	,							type=A_	,							required=A_       )
       expected.add_argument("--bar"	,							type=A_	,							required=A_       )
       expected.add_argument("--baz"	,							type=A_	,							required=A_       )
       expected.add_argument("--flag"	,							type=A_	,							default=A_	,							const=A_	,							nargs="?"       )
       self.argparsersEqual(A_	,							A_       )

       UpperCamelCase     : List[str]	     =       ["--foo", "1", "--baz", "quux", "--bar", "0.5"]
       ((UpperCamelCase)     ,   )     : List[str]	     =       parser.parse_args_into_dataclasses(A_	,							look_for_args_file=A_       )
       self.assertFalse(example.flag       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[int]	     =       HfArgumentParser(A_       )

       UpperCamelCase     : Tuple	     =       argparse.ArgumentParser()
       expected.add_argument("--foo"	,							default=42	,							type=A_       )
       expected.add_argument("--baz"	,							default="toto"	,							type=A_	,							help="help message"       )
       self.argparsersEqual(A_	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       argparse.ArgumentParser()
       expected.add_argument("--foo"	,							type=A_	,							default=A_	,							const=A_	,							nargs="?"       )
       expected.add_argument("--baz"	,							type=A_	,							default=A_	,							const=A_	,							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=A_	,							dest="baz"       )
       expected.add_argument("--opt"	,							type=A_	,							default=A_       )

       UpperCamelCase     : Any	     =       [WithDefaultBoolExample]
       if is_python_no_less_than_3_10:
        dataclass_types.append(A_       )

       for dataclass_type in dataclass_types:
        UpperCamelCase     : Optional[Any]	     =       HfArgumentParser(A_       )
        self.argparsersEqual(A_	,							A_       )

        UpperCamelCase     : str	     =       parser.parse_args([]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							baz=A_	,							opt=A_       )       )

        UpperCamelCase     : int	     =       parser.parse_args(["--foo", "--no_baz"]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							baz=A_	,							opt=A_       )       )

        UpperCamelCase     : Optional[int]	     =       parser.parse_args(["--foo", "--baz"]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							baz=A_	,							opt=A_       )       )

        UpperCamelCase     : List[Any]	     =       parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							baz=A_	,							opt=A_       )       )

        UpperCamelCase     : Dict	     =       parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							baz=A_	,							opt=A_       )       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : str	     =       HfArgumentParser(A_       )

       UpperCamelCase     : Dict	     =       argparse.ArgumentParser()
       expected.add_argument(
           "--foo"	,							default="toto"	,							choices=["titi", "toto", 42]	,							type=make_choice_type_function(["titi", "toto", 42]       )	,							)
       self.argparsersEqual(A_	,							A_       )

       UpperCamelCase     : Tuple	     =       parser.parse_args([]       )
       self.assertEqual(args.foo	,							"toto"       )
       UpperCamelCase     : List[Any]	     =       parser.parse_args_into_dataclasses([]       )[0]
       self.assertEqual(enum_ex.foo	,							MixedTypeEnum.toto       )

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

       UpperCamelCase     : List[str]	     =       parser.parse_args(["--foo", "42"]       )
       self.assertEqual(args.foo	,							42       )
       UpperCamelCase     : Optional[Any]	     =       parser.parse_args_into_dataclasses(["--foo", "42"]       )[0]
       self.assertEqual(enum_ex.foo	,							MixedTypeEnum.fourtytwo       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       @dataclass
       class  A__       :
             _UpperCAmelCase     :Literal["titi", "toto", 4_2]						   =       "toto"


       UpperCamelCase     : int	     =       HfArgumentParser(A_       )

       UpperCamelCase     : Any	     =       argparse.ArgumentParser()
       expected.add_argument(
           "--foo"	,							default="toto"	,							choices=("titi", "toto", 42)	,							type=make_choice_type_function(["titi", "toto", 42]       )	,							)
       self.argparsersEqual(A_	,							A_       )

       UpperCamelCase     : Tuple	     =       parser.parse_args([]       )
       self.assertEqual(args.foo	,							"toto"       )

       UpperCamelCase     : Union[str, Any]	     =       parser.parse_args(["--foo", "titi"]       )
       self.assertEqual(args.foo	,							"titi"       )

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Any	     =       HfArgumentParser(A_       )

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

       self.argparsersEqual(A_	,							A_       )

       UpperCamelCase     : Union[str, Any]	     =       parser.parse_args([]       )
       self.assertEqual(
           A_	,							Namespace(foo_int=[]	,							bar_int=[1, 2, 3]	,							foo_str=["Hallo", "Bonjour", "Hello"]	,							foo_float=[0.1, 0.2, 0.3]       )	,							)

       UpperCamelCase     : Optional[Any]	     =       parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split()       )
       self.assertEqual(A_	,							Namespace(foo_int=[1]	,							bar_int=[2, 3]	,							foo_str=["a", "b", "c"]	,							foo_float=[0.1, 0.7]       )       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Dict	     =       argparse.ArgumentParser()
       expected.add_argument("--foo"	,							default=A_	,							type=A_       )
       expected.add_argument("--bar"	,							default=A_	,							type=A_	,							help="help message"       )
       expected.add_argument("--baz"	,							default=A_	,							type=A_       )
       expected.add_argument("--ces"	,							nargs="+"	,							default=[]	,							type=A_       )
       expected.add_argument("--des"	,							nargs="+"	,							default=[]	,							type=A_       )

       UpperCamelCase     : str	     =       [OptionalExample]
       if is_python_no_less_than_3_10:
        dataclass_types.append(A_       )

       for dataclass_type in dataclass_types:
        UpperCamelCase     : List[Any]	     =       HfArgumentParser(A_       )

        self.argparsersEqual(A_	,							A_       )

        UpperCamelCase     : Union[str, Any]	     =       parser.parse_args([]       )
        self.assertEqual(A_	,							Namespace(foo=A_	,							bar=A_	,							baz=A_	,							ces=[]	,							des=[]       )       )

        UpperCamelCase     : str	     =       parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split()       )
        self.assertEqual(A_	,							Namespace(foo=12	,							bar=3.14	,							baz="42"	,							ces=["a", "b", "c"]	,							des=[1, 2, 3]       )       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       HfArgumentParser(A_       )

       UpperCamelCase     : List[str]	     =       argparse.ArgumentParser()
       expected.add_argument("--required_list"	,							nargs="+"	,							type=A_	,							required=A_       )
       expected.add_argument("--required_str"	,							type=A_	,							required=A_       )
       expected.add_argument(
           "--required_enum"	,							type=make_choice_type_function(["titi", "toto"]       )	,							choices=["titi", "toto"]	,							required=A_	,							)
       self.argparsersEqual(A_	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[int]	     =       HfArgumentParser(A_       )

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : List[str]	     =       HfArgumentParser(A_       )

       UpperCamelCase     : Tuple	     =       {
           "foo": 12,
           "bar": 3.14,
           "baz": "42",
           "flag": True,
       }

       UpperCamelCase     : int	     =       parser.parse_dict(A_       )[0]
       UpperCamelCase     : str	     =       BasicExample(**A_       )
       self.assertEqual(A_	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[Any]	     =       HfArgumentParser(A_       )

       UpperCamelCase     : Optional[Any]	     =       {
           "foo": 12,
           "bar": 3.14,
           "baz": "42",
           "flag": True,
           "extra": 42,
       }

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       HfArgumentParser(A_       )

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

       UpperCamelCase     : Any	     =       BasicExample(**A_       )
       self.assertEqual(A_	,							A_       )





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : int	     =       HfArgumentParser(A_       )

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





      def 	__UpperCamelCase(			self       ):
       '''simple docstring'''




       UpperCamelCase     : Union[str, Any]	     =       HfArgumentParser(A_       )
       self.assertIsNotNone(A_       )







class  A__       :  # Public class to implement a graph





      def __init__(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Optional[int]	     =       row
       UpperCamelCase     : Any	     =       col
       UpperCamelCase     : Optional[Any]	     =       graph





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       return (
           0 <= i < self.ROW
           and 0 <= j < self.COL
           and not visited[i][j]
           and self.graph[i][j]
       )





      def 	__UpperCamelCase(			self	,							A_	,							A_	,							A_       ):
       '''simple docstring'''




       UpperCamelCase     : Any	     =       [-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
       UpperCamelCase     : Dict	     =       [-1, 0, 1, -1, 1, -1, 0, 1]
       UpperCamelCase     : Any	     =       True  # Make those cells visited
       for k in range(8       ):
        if self.is_safe(i + row_nbr[k]	,							j + col_nbr[k]	,							A_       ):
         self.diffs(i + row_nbr[k]	,							j + col_nbr[k]	,							A_       )





      def 	__UpperCamelCase(			self       ):  # And finally, count all islands.
       '''simple docstring'''




       UpperCamelCase     : str	     =       [[False for j in range(self.COL       )] for i in range(self.ROW       )]
       UpperCamelCase     : int	     =       0
       for i in range(self.ROW       ):
        for j in range(self.COL       ):
         if visited[i][j] is False and self.graph[i][j] == 1:
          self.diffs(A_	,							A_	,							A_       )
          count += 1
       return count
