"git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "1144d336b689d1710534b245697e41be7a168075"
Unverified Commit 8fd2de93 authored by Xu Song's avatar Xu Song Committed by GitHub
Browse files

Add test for parse_json_file and change typing to os.PathLike (#30183)

* Add test for parse_json_file

* Change Path to PathLike

* Fix `Import block is un-sorted or un-formatted`

* revert parse_json_file

* Fix ruff format

* Add parse_json_file test
parent b109257f
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
import dataclasses import dataclasses
import json import json
import os
import sys import sys
import types import types
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError
...@@ -376,7 +377,9 @@ class HfArgumentParser(ArgumentParser): ...@@ -376,7 +377,9 @@ class HfArgumentParser(ArgumentParser):
raise ValueError(f"Some keys are not used by the HfArgumentParser: {sorted(unused_keys)}") raise ValueError(f"Some keys are not used by the HfArgumentParser: {sorted(unused_keys)}")
return tuple(outputs) return tuple(outputs)
def parse_json_file(self, json_file: Union[str, Path], allow_extra_keys: bool = False) -> Tuple[DataClass, ...]: def parse_json_file(
self, json_file: Union[str, os.PathLike], allow_extra_keys: bool = False
) -> Tuple[DataClass, ...]:
""" """
Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the
dataclass types. dataclass types.
...@@ -398,7 +401,9 @@ class HfArgumentParser(ArgumentParser): ...@@ -398,7 +401,9 @@ class HfArgumentParser(ArgumentParser):
outputs = self.parse_dict(data, allow_extra_keys=allow_extra_keys) outputs = self.parse_dict(data, allow_extra_keys=allow_extra_keys)
return tuple(outputs) return tuple(outputs)
def parse_yaml_file(self, yaml_file: Union[str, Path], allow_extra_keys: bool = False) -> Tuple[DataClass, ...]: def parse_yaml_file(
self, yaml_file: Union[str, os.PathLike], allow_extra_keys: bool = False
) -> Tuple[DataClass, ...]:
""" """
Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the
dataclass types. dataclass types.
......
...@@ -379,7 +379,7 @@ class HfArgumentParserTest(unittest.TestCase): ...@@ -379,7 +379,7 @@ class HfArgumentParserTest(unittest.TestCase):
os.mkdir(temp_local_path) os.mkdir(temp_local_path)
with open(temp_local_path + ".json", "w+") as f: with open(temp_local_path + ".json", "w+") as f:
json.dump(args_dict_for_json, f) json.dump(args_dict_for_json, f)
parsed_args = parser.parse_yaml_file(Path(temp_local_path + ".json"))[0] parsed_args = parser.parse_json_file(Path(temp_local_path + ".json"))[0]
args = BasicExample(**args_dict_for_json) args = BasicExample(**args_dict_for_json)
self.assertEqual(parsed_args, args) self.assertEqual(parsed_args, args)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment