We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Error in run_tf_squad.py script
## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 3.0.2
- Platform: Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.6.9
- PyTorch version (GPU?): 1.6.0+cu101 (True)
- Tensorflow version (GPU?): 2.3.0 (True)
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

### Who can help
<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.
 
 albert, bert, GPT2, XLM: @LysandreJik 
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Speed and Memory Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 Translation: @sshleifer
 Summarization: @sshleifer
 TextGeneration: @TevenLeScao 
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @TevenLeScao
 blenderbot: @mariamabarham
 Bart: @sshleifer
 Marian: @sshleifer
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao 
 examples/seq2seq: @sshleifer
 tensorflow: @jplu 
documentation: @sgugger
 --> @sgugger

## Information

Model I am using (Bert, XLNet ...):

The problem arises when using:
* [x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

The tasks I am working on is:
* [x] an official GLUE/SQUaD task: SQUaD
* [ ] my own task or dataset: (give details below)

I'm simply trying to train a new question answering model using the TF trainer script, and I get the following error:
```python
Traceback (most recent call last):
  File "run_tf_squad.py", line 244, in <module>
    main()
  File "run_tf_squad.py", line 123, in main
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments))
  File "/usr/local/lib/python3.6/dist-packages/transformers/hf_argparser.py", line 40, in __init__
    self._add_dataclass_arguments(dtype)
  File "/usr/local/lib/python3.6/dist-packages/transformers/hf_argparser.py", line 72, in _add_dataclass_arguments
    elif hasattr(field.type, "__origin__") and issubclass(field.type.__origin__, List):
  File "/usr/lib/python3.6/typing.py", line 1154, in __subclasscheck__
    return super().__subclasscheck__(cls)
  File "/usr/lib/python3.6/abc.py", line 209, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
  File "/usr/lib/python3.6/typing.py", line 890, in __extrahook__
    if cls.__extra__ and issubclass(subclass, cls.__extra__):
TypeError: issubclass() arg 1 must be a class
```

## To reproduce

Steps to reproduce the behavior:

1.install transformers from the master branch
2.run the example script in question-answering:
```
python run_tf_squad.py \
    --model_name_or_path bert-base-uncased \
    --output_dir model \
    --max_seq_length 384 \
    --num_train_epochs 2 \
    --per_gpu_train_batch_size 8 \
    --per_gpu_eval_batch_size 16 \
    --do_train \
    --logging_dir logs \    
    --logging_steps 10 \
    --learning_rate 3e-5 \
    --doc_stride 128    
```

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior
The script should run normally and train the model
<!-- A clear and concise description of what you would expect to happen. -->


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/transformers/hf_argparser.py`
Content:
```
1 import dataclasses
2 import json
3 import sys
4 from argparse import ArgumentParser
5 from enum import Enum
6 from pathlib import Path
7 from typing import Any, Iterable, List, NewType, Tuple, Union
8 
9 
10 DataClass = NewType("DataClass", Any)
11 DataClassType = NewType("DataClassType", Any)
12 
13 
14 class HfArgumentParser(ArgumentParser):
15     """
16     This subclass of `argparse.ArgumentParser` uses type hints on dataclasses
17     to generate arguments.
18 
19     The class is designed to play well with the native argparse. In particular,
20     you can add more (non-dataclass backed) arguments to the parser after initialization
21     and you'll get the output back after parsing as an additional namespace.
22     """
23 
24     dataclass_types: Iterable[DataClassType]
25 
26     def __init__(self, dataclass_types: Union[DataClassType, Iterable[DataClassType]], **kwargs):
27         """
28         Args:
29             dataclass_types:
30                 Dataclass type, or list of dataclass types for which we will "fill" instances
31                 with the parsed args.
32             kwargs:
33                 (Optional) Passed to `argparse.ArgumentParser()` in the regular way.
34         """
35         super().__init__(**kwargs)
36         if dataclasses.is_dataclass(dataclass_types):
37             dataclass_types = [dataclass_types]
38         self.dataclass_types = dataclass_types
39         for dtype in self.dataclass_types:
40             self._add_dataclass_arguments(dtype)
41 
42     def _add_dataclass_arguments(self, dtype: DataClassType):
43         for field in dataclasses.fields(dtype):
44             field_name = f"--{field.name}"
45             kwargs = field.metadata.copy()
46             # field.metadata is not used at all by Data Classes,
47             # it is provided as a third-party extension mechanism.
48             if isinstance(field.type, str):
49                 raise ImportError(
50                     "This implementation is not compatible with Postponed Evaluation of Annotations (PEP 563),"
51                     "which can be opted in from Python 3.7 with `from __future__ import annotations`."
52                     "We will add compatibility when Python 3.9 is released."
53                 )
54             typestring = str(field.type)
55             for prim_type in (int, float, str):
56                 for collection in (List,):
57                     if typestring == f"typing.Union[{collection[prim_type]}, NoneType]":
58                         field.type = collection[prim_type]
59                 if typestring == f"typing.Union[{prim_type.__name__}, NoneType]":
60                     field.type = prim_type
61 
62             if isinstance(field.type, type) and issubclass(field.type, Enum):
63                 kwargs["choices"] = list(field.type)
64                 kwargs["type"] = field.type
65                 if field.default is not dataclasses.MISSING:
66                     kwargs["default"] = field.default
67             elif field.type is bool:
68                 kwargs["action"] = "store_false" if field.default is True else "store_true"
69                 if field.default is True:
70                     field_name = f"--no-{field.name}"
71                     kwargs["dest"] = field.name
72             elif hasattr(field.type, "__origin__") and issubclass(field.type.__origin__, List):
73                 kwargs["nargs"] = "+"
74                 kwargs["type"] = field.type.__args__[0]
75                 assert all(
76                     x == kwargs["type"] for x in field.type.__args__
77                 ), "{} cannot be a List of mixed types".format(field.name)
78                 if field.default_factory is not dataclasses.MISSING:
79                     kwargs["default"] = field.default_factory()
80             else:
81                 kwargs["type"] = field.type
82                 if field.default is not dataclasses.MISSING:
83                     kwargs["default"] = field.default
84                 elif field.default_factory is not dataclasses.MISSING:
85                     kwargs["default"] = field.default_factory()
86                 else:
87                     kwargs["required"] = True
88             self.add_argument(field_name, **kwargs)
89 
90     def parse_args_into_dataclasses(
91         self, args=None, return_remaining_strings=False, look_for_args_file=True
92     ) -> Tuple[DataClass, ...]:
93         """
94         Parse command-line args into instances of the specified dataclass types.
95 
96         This relies on argparse's `ArgumentParser.parse_known_args`.
97         See the doc at:
98         docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args
99 
100         Args:
101             args:
102                 List of strings to parse. The default is taken from sys.argv.
103                 (same as argparse.ArgumentParser)
104             return_remaining_strings:
105                 If true, also return a list of remaining argument strings.
106             look_for_args_file:
107                 If true, will look for a ".args" file with the same base name
108                 as the entry point script for this process, and will append its
109                 potential content to the command line args.
110 
111         Returns:
112             Tuple consisting of:
113                 - the dataclass instances in the same order as they
114                   were passed to the initializer.abspath
115                 - if applicable, an additional namespace for more
116                   (non-dataclass backed) arguments added to the parser
117                   after initialization.
118                 - The potential list of remaining argument strings.
119                   (same as argparse.ArgumentParser.parse_known_args)
120         """
121         if look_for_args_file and len(sys.argv):
122             args_file = Path(sys.argv[0]).with_suffix(".args")
123             if args_file.exists():
124                 fargs = args_file.read_text().split()
125                 args = fargs + args if args is not None else fargs + sys.argv[1:]
126                 # in case of duplicate arguments the first one has precedence
127                 # so we append rather than prepend.
128         namespace, remaining_args = self.parse_known_args(args=args)
129         outputs = []
130         for dtype in self.dataclass_types:
131             keys = {f.name for f in dataclasses.fields(dtype)}
132             inputs = {k: v for k, v in vars(namespace).items() if k in keys}
133             for k in keys:
134                 delattr(namespace, k)
135             obj = dtype(**inputs)
136             outputs.append(obj)
137         if len(namespace.__dict__) > 0:
138             # additional namespace.
139             outputs.append(namespace)
140         if return_remaining_strings:
141             return (*outputs, remaining_args)
142         else:
143             if remaining_args:
144                 raise ValueError(f"Some specified arguments are not used by the HfArgumentParser: {remaining_args}")
145 
146             return (*outputs,)
147 
148     def parse_json_file(self, json_file: str) -> Tuple[DataClass, ...]:
149         """
150         Alternative helper method that does not use `argparse` at all,
151         instead loading a json file and populating the dataclass types.
152         """
153         data = json.loads(Path(json_file).read_text())
154         outputs = []
155         for dtype in self.dataclass_types:
156             keys = {f.name for f in dataclasses.fields(dtype)}
157             inputs = {k: v for k, v in data.items() if k in keys}
158             obj = dtype(**inputs)
159             outputs.append(obj)
160         return (*outputs,)
161 
162     def parse_dict(self, args: dict) -> Tuple[DataClass, ...]:
163         """
164         Alternative helper method that does not use `argparse` at all,
165         instead uses a dict and populating the dataclass types.
166         """
167         outputs = []
168         for dtype in self.dataclass_types:
169             keys = {f.name for f in dataclasses.fields(dtype)}
170             inputs = {k: v for k, v in args.items() if k in keys}
171             obj = dtype(**inputs)
172             outputs.append(obj)
173         return (*outputs,)
174
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/transformers/hf_argparser.py b/src/transformers/hf_argparser.py
--- a/src/transformers/hf_argparser.py
+++ b/src/transformers/hf_argparser.py
@@ -4,7 +4,7 @@
 from argparse import ArgumentParser
 from enum import Enum
 from pathlib import Path
-from typing import Any, Iterable, List, NewType, Tuple, Union
+from typing import Any, Iterable, List, NewType, Optional, Tuple, Union
 
 
 DataClass = NewType("DataClass", Any)
@@ -64,7 +64,7 @@
                 kwargs["type"] = field.type
                 if field.default is not dataclasses.MISSING:
                     kwargs["default"] = field.default
-            elif field.type is bool:
+            elif field.type is bool or field.type is Optional[bool]:
                 kwargs["action"] = "store_false" if field.default is True else "store_true"
                 if field.default is True:
                     field_name = f"--no-{field.name}"

{"golden_diff": "diff --git a/src/transformers/hf_argparser.py b/src/transformers/hf_argparser.py\n--- a/src/transformers/hf_argparser.py\n+++ b/src/transformers/hf_argparser.py\n@@ -4,7 +4,7 @@\n from argparse import ArgumentParser\n from enum import Enum\n from pathlib import Path\n-from typing import Any, Iterable, List, NewType, Tuple, Union\n+from typing import Any, Iterable, List, NewType, Optional, Tuple, Union\n \n \n DataClass = NewType(\"DataClass\", Any)\n@@ -64,7 +64,7 @@\n                 kwargs[\"type\"] = field.type\n                 if field.default is not dataclasses.MISSING:\n                     kwargs[\"default\"] = field.default\n-            elif field.type is bool:\n+            elif field.type is bool or field.type is Optional[bool]:\n                 kwargs[\"action\"] = \"store_false\" if field.default is True else \"store_true\"\n                 if field.default is True:\n                     field_name = f\"--no-{field.name}\"\n", "issue": "Error in run_tf_squad.py script\n## Environment info\r\n<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.\r\n     Don't forget to fill out the missing fields in that output! -->\r\n     \r\n- `transformers` version: 3.0.2\r\n- Platform: Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic\r\n- Python version: 3.6.9\r\n- PyTorch version (GPU?): 1.6.0+cu101 (True)\r\n- Tensorflow version (GPU?): 2.3.0 (True)\r\n- Using GPU in script?: Yes\r\n- Using distributed or parallel set-up in script?: No\r\n\r\n### Who can help\r\n<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @\r\n If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.\r\n Please tag fewer than 3 people.\r\n \r\n albert, bert, GPT2, XLM: @LysandreJik \r\n tokenizers: @mfuntowicz\r\n Trainer: @sgugger\r\n Speed and Memory Benchmarks: @patrickvonplaten\r\n Model Cards: @julien-c\r\n Translation: @sshleifer\r\n Summarization: @sshleifer\r\n TextGeneration: @TevenLeScao \r\n examples/distillation: @VictorSanh\r\n nlp datasets: [different repo](https://github.com/huggingface/nlp)\r\n rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)\r\n Text Generation: @TevenLeScao\r\n blenderbot: @mariamabarham\r\n Bart: @sshleifer\r\n Marian: @sshleifer\r\n T5: @patrickvonplaten\r\n Longformer/Reformer: @patrickvonplaten\r\n TransfoXL/XLNet: @TevenLeScao \r\n examples/seq2seq: @sshleifer\r\n tensorflow: @jplu \r\ndocumentation: @sgugger\r\n --> @sgugger\r\n\r\n## Information\r\n\r\nModel I am using (Bert, XLNet ...):\r\n\r\nThe problem arises when using:\r\n* [x] the official example scripts: (give details below)\r\n* [ ] my own modified scripts: (give details below)\r\n\r\nThe tasks I am working on is:\r\n* [x] an official GLUE/SQUaD task: SQUaD\r\n* [ ] my own task or dataset: (give details below)\r\n\r\nI'm simply trying to train a new question answering model using the TF trainer script, and I get the following error:\r\n```python\r\nTraceback (most recent call last):\r\n  File \"run_tf_squad.py\", line 244, in <module>\r\n    main()\r\n  File \"run_tf_squad.py\", line 123, in main\r\n    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments))\r\n  File \"/usr/local/lib/python3.6/dist-packages/transformers/hf_argparser.py\", line 40, in __init__\r\n    self._add_dataclass_arguments(dtype)\r\n  File \"/usr/local/lib/python3.6/dist-packages/transformers/hf_argparser.py\", line 72, in _add_dataclass_arguments\r\n    elif hasattr(field.type, \"__origin__\") and issubclass(field.type.__origin__, List):\r\n  File \"/usr/lib/python3.6/typing.py\", line 1154, in __subclasscheck__\r\n    return super().__subclasscheck__(cls)\r\n  File \"/usr/lib/python3.6/abc.py\", line 209, in __subclasscheck__\r\n    ok = cls.__subclasshook__(subclass)\r\n  File \"/usr/lib/python3.6/typing.py\", line 890, in __extrahook__\r\n    if cls.__extra__ and issubclass(subclass, cls.__extra__):\r\nTypeError: issubclass() arg 1 must be a class\r\n```\r\n\r\n## To reproduce\r\n\r\nSteps to reproduce the behavior:\r\n\r\n1.install transformers from the master branch\r\n2.run the example script in question-answering:\r\n```\r\npython run_tf_squad.py \\\r\n    --model_name_or_path bert-base-uncased \\\r\n    --output_dir model \\\r\n    --max_seq_length 384 \\\r\n    --num_train_epochs 2 \\\r\n    --per_gpu_train_batch_size 8 \\\r\n    --per_gpu_eval_batch_size 16 \\\r\n    --do_train \\\r\n    --logging_dir logs \\    \r\n    --logging_steps 10 \\\r\n    --learning_rate 3e-5 \\\r\n    --doc_stride 128    \r\n```\r\n\r\n<!-- If you have code snippets, error messages, stack traces please provide them here as well.\r\n     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting\r\n     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->\r\n\r\n## Expected behavior\r\nThe script should run normally and train the model\r\n<!-- A clear and concise description of what you would expect to happen. -->\r\n\n", "before_files": [{"content": "import dataclasses\nimport json\nimport sys\nfrom argparse import ArgumentParser\nfrom enum import Enum\nfrom pathlib import Path\nfrom typing import Any, Iterable, List, NewType, Tuple, Union\n\n\nDataClass = NewType(\"DataClass\", Any)\nDataClassType = NewType(\"DataClassType\", Any)\n\n\nclass HfArgumentParser(ArgumentParser):\n    \"\"\"\n    This subclass of `argparse.ArgumentParser` uses type hints on dataclasses\n    to generate arguments.\n\n    The class is designed to play well with the native argparse. In particular,\n    you can add more (non-dataclass backed) arguments to the parser after initialization\n    and you'll get the output back after parsing as an additional namespace.\n    \"\"\"\n\n    dataclass_types: Iterable[DataClassType]\n\n    def __init__(self, dataclass_types: Union[DataClassType, Iterable[DataClassType]], **kwargs):\n        \"\"\"\n        Args:\n            dataclass_types:\n                Dataclass type, or list of dataclass types for which we will \"fill\" instances\n                with the parsed args.\n            kwargs:\n                (Optional) Passed to `argparse.ArgumentParser()` in the regular way.\n        \"\"\"\n        super().__init__(**kwargs)\n        if dataclasses.is_dataclass(dataclass_types):\n            dataclass_types = [dataclass_types]\n        self.dataclass_types = dataclass_types\n        for dtype in self.dataclass_types:\n            self._add_dataclass_arguments(dtype)\n\n    def _add_dataclass_arguments(self, dtype: DataClassType):\n        for field in dataclasses.fields(dtype):\n            field_name = f\"--{field.name}\"\n            kwargs = field.metadata.copy()\n            # field.metadata is not used at all by Data Classes,\n            # it is provided as a third-party extension mechanism.\n            if isinstance(field.type, str):\n                raise ImportError(\n                    \"This implementation is not compatible with Postponed Evaluation of Annotations (PEP 563),\"\n                    \"which can be opted in from Python 3.7 with `from __future__ import annotations`.\"\n                    \"We will add compatibility when Python 3.9 is released.\"\n                )\n            typestring = str(field.type)\n            for prim_type in (int, float, str):\n                for collection in (List,):\n                    if typestring == f\"typing.Union[{collection[prim_type]}, NoneType]\":\n                        field.type = collection[prim_type]\n                if typestring == f\"typing.Union[{prim_type.__name__}, NoneType]\":\n                    field.type = prim_type\n\n            if isinstance(field.type, type) and issubclass(field.type, Enum):\n                kwargs[\"choices\"] = list(field.type)\n                kwargs[\"type\"] = field.type\n                if field.default is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default\n            elif field.type is bool:\n                kwargs[\"action\"] = \"store_false\" if field.default is True else \"store_true\"\n                if field.default is True:\n                    field_name = f\"--no-{field.name}\"\n                    kwargs[\"dest\"] = field.name\n            elif hasattr(field.type, \"__origin__\") and issubclass(field.type.__origin__, List):\n                kwargs[\"nargs\"] = \"+\"\n                kwargs[\"type\"] = field.type.__args__[0]\n                assert all(\n                    x == kwargs[\"type\"] for x in field.type.__args__\n                ), \"{} cannot be a List of mixed types\".format(field.name)\n                if field.default_factory is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default_factory()\n            else:\n                kwargs[\"type\"] = field.type\n                if field.default is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default\n                elif field.default_factory is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default_factory()\n                else:\n                    kwargs[\"required\"] = True\n            self.add_argument(field_name, **kwargs)\n\n    def parse_args_into_dataclasses(\n        self, args=None, return_remaining_strings=False, look_for_args_file=True\n    ) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Parse command-line args into instances of the specified dataclass types.\n\n        This relies on argparse's `ArgumentParser.parse_known_args`.\n        See the doc at:\n        docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args\n\n        Args:\n            args:\n                List of strings to parse. The default is taken from sys.argv.\n                (same as argparse.ArgumentParser)\n            return_remaining_strings:\n                If true, also return a list of remaining argument strings.\n            look_for_args_file:\n                If true, will look for a \".args\" file with the same base name\n                as the entry point script for this process, and will append its\n                potential content to the command line args.\n\n        Returns:\n            Tuple consisting of:\n                - the dataclass instances in the same order as they\n                  were passed to the initializer.abspath\n                - if applicable, an additional namespace for more\n                  (non-dataclass backed) arguments added to the parser\n                  after initialization.\n                - The potential list of remaining argument strings.\n                  (same as argparse.ArgumentParser.parse_known_args)\n        \"\"\"\n        if look_for_args_file and len(sys.argv):\n            args_file = Path(sys.argv[0]).with_suffix(\".args\")\n            if args_file.exists():\n                fargs = args_file.read_text().split()\n                args = fargs + args if args is not None else fargs + sys.argv[1:]\n                # in case of duplicate arguments the first one has precedence\n                # so we append rather than prepend.\n        namespace, remaining_args = self.parse_known_args(args=args)\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in vars(namespace).items() if k in keys}\n            for k in keys:\n                delattr(namespace, k)\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        if len(namespace.__dict__) > 0:\n            # additional namespace.\n            outputs.append(namespace)\n        if return_remaining_strings:\n            return (*outputs, remaining_args)\n        else:\n            if remaining_args:\n                raise ValueError(f\"Some specified arguments are not used by the HfArgumentParser: {remaining_args}\")\n\n            return (*outputs,)\n\n    def parse_json_file(self, json_file: str) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Alternative helper method that does not use `argparse` at all,\n        instead loading a json file and populating the dataclass types.\n        \"\"\"\n        data = json.loads(Path(json_file).read_text())\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in data.items() if k in keys}\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        return (*outputs,)\n\n    def parse_dict(self, args: dict) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Alternative helper method that does not use `argparse` at all,\n        instead uses a dict and populating the dataclass types.\n        \"\"\"\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in args.items() if k in keys}\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        return (*outputs,)\n", "path": "src/transformers/hf_argparser.py"}], "after_files": [{"content": "import dataclasses\nimport json\nimport sys\nfrom argparse import ArgumentParser\nfrom enum import Enum\nfrom pathlib import Path\nfrom typing import Any, Iterable, List, NewType, Optional, Tuple, Union\n\n\nDataClass = NewType(\"DataClass\", Any)\nDataClassType = NewType(\"DataClassType\", Any)\n\n\nclass HfArgumentParser(ArgumentParser):\n    \"\"\"\n    This subclass of `argparse.ArgumentParser` uses type hints on dataclasses\n    to generate arguments.\n\n    The class is designed to play well with the native argparse. In particular,\n    you can add more (non-dataclass backed) arguments to the parser after initialization\n    and you'll get the output back after parsing as an additional namespace.\n    \"\"\"\n\n    dataclass_types: Iterable[DataClassType]\n\n    def __init__(self, dataclass_types: Union[DataClassType, Iterable[DataClassType]], **kwargs):\n        \"\"\"\n        Args:\n            dataclass_types:\n                Dataclass type, or list of dataclass types for which we will \"fill\" instances\n                with the parsed args.\n            kwargs:\n                (Optional) Passed to `argparse.ArgumentParser()` in the regular way.\n        \"\"\"\n        super().__init__(**kwargs)\n        if dataclasses.is_dataclass(dataclass_types):\n            dataclass_types = [dataclass_types]\n        self.dataclass_types = dataclass_types\n        for dtype in self.dataclass_types:\n            self._add_dataclass_arguments(dtype)\n\n    def _add_dataclass_arguments(self, dtype: DataClassType):\n        for field in dataclasses.fields(dtype):\n            field_name = f\"--{field.name}\"\n            kwargs = field.metadata.copy()\n            # field.metadata is not used at all by Data Classes,\n            # it is provided as a third-party extension mechanism.\n            if isinstance(field.type, str):\n                raise ImportError(\n                    \"This implementation is not compatible with Postponed Evaluation of Annotations (PEP 563),\"\n                    \"which can be opted in from Python 3.7 with `from __future__ import annotations`.\"\n                    \"We will add compatibility when Python 3.9 is released.\"\n                )\n            typestring = str(field.type)\n            for prim_type in (int, float, str):\n                for collection in (List,):\n                    if typestring == f\"typing.Union[{collection[prim_type]}, NoneType]\":\n                        field.type = collection[prim_type]\n                if typestring == f\"typing.Union[{prim_type.__name__}, NoneType]\":\n                    field.type = prim_type\n\n            if isinstance(field.type, type) and issubclass(field.type, Enum):\n                kwargs[\"choices\"] = list(field.type)\n                kwargs[\"type\"] = field.type\n                if field.default is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default\n            elif field.type is bool or field.type is Optional[bool]:\n                kwargs[\"action\"] = \"store_false\" if field.default is True else \"store_true\"\n                if field.default is True:\n                    field_name = f\"--no-{field.name}\"\n                    kwargs[\"dest\"] = field.name\n            elif hasattr(field.type, \"__origin__\") and issubclass(field.type.__origin__, List):\n                kwargs[\"nargs\"] = \"+\"\n                kwargs[\"type\"] = field.type.__args__[0]\n                assert all(\n                    x == kwargs[\"type\"] for x in field.type.__args__\n                ), \"{} cannot be a List of mixed types\".format(field.name)\n                if field.default_factory is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default_factory()\n            else:\n                kwargs[\"type\"] = field.type\n                if field.default is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default\n                elif field.default_factory is not dataclasses.MISSING:\n                    kwargs[\"default\"] = field.default_factory()\n                else:\n                    kwargs[\"required\"] = True\n            self.add_argument(field_name, **kwargs)\n\n    def parse_args_into_dataclasses(\n        self, args=None, return_remaining_strings=False, look_for_args_file=True\n    ) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Parse command-line args into instances of the specified dataclass types.\n\n        This relies on argparse's `ArgumentParser.parse_known_args`.\n        See the doc at:\n        docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args\n\n        Args:\n            args:\n                List of strings to parse. The default is taken from sys.argv.\n                (same as argparse.ArgumentParser)\n            return_remaining_strings:\n                If true, also return a list of remaining argument strings.\n            look_for_args_file:\n                If true, will look for a \".args\" file with the same base name\n                as the entry point script for this process, and will append its\n                potential content to the command line args.\n\n        Returns:\n            Tuple consisting of:\n                - the dataclass instances in the same order as they\n                  were passed to the initializer.abspath\n                - if applicable, an additional namespace for more\n                  (non-dataclass backed) arguments added to the parser\n                  after initialization.\n                - The potential list of remaining argument strings.\n                  (same as argparse.ArgumentParser.parse_known_args)\n        \"\"\"\n        if look_for_args_file and len(sys.argv):\n            args_file = Path(sys.argv[0]).with_suffix(\".args\")\n            if args_file.exists():\n                fargs = args_file.read_text().split()\n                args = fargs + args if args is not None else fargs + sys.argv[1:]\n                # in case of duplicate arguments the first one has precedence\n                # so we append rather than prepend.\n        namespace, remaining_args = self.parse_known_args(args=args)\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in vars(namespace).items() if k in keys}\n            for k in keys:\n                delattr(namespace, k)\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        if len(namespace.__dict__) > 0:\n            # additional namespace.\n            outputs.append(namespace)\n        if return_remaining_strings:\n            return (*outputs, remaining_args)\n        else:\n            if remaining_args:\n                raise ValueError(f\"Some specified arguments are not used by the HfArgumentParser: {remaining_args}\")\n\n            return (*outputs,)\n\n    def parse_json_file(self, json_file: str) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Alternative helper method that does not use `argparse` at all,\n        instead loading a json file and populating the dataclass types.\n        \"\"\"\n        data = json.loads(Path(json_file).read_text())\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in data.items() if k in keys}\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        return (*outputs,)\n\n    def parse_dict(self, args: dict) -> Tuple[DataClass, ...]:\n        \"\"\"\n        Alternative helper method that does not use `argparse` at all,\n        instead uses a dict and populating the dataclass types.\n        \"\"\"\n        outputs = []\n        for dtype in self.dataclass_types:\n            keys = {f.name for f in dataclasses.fields(dtype)}\n            inputs = {k: v for k, v in args.items() if k in keys}\n            obj = dtype(**inputs)\n            outputs.append(obj)\n        return (*outputs,)\n", "path": "src/transformers/hf_argparser.py"}]}