MCPcopy
hub / github.com/huggingface/transformers / parse_dict

Method parse_dict

src/transformers/hf_argparser.py:358–384  ·  view source on GitHub ↗

Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass types. Args: args (`dict`): dict containing config values allow_extra_keys (`bool`, *optional*, defaults to `False`):

(self, args: dict[str, Any], allow_extra_keys: bool = False)

Source from the content-addressed store, hash-verified

356 return (*outputs,)
357
358 def parse_dict(self, args: dict[str, Any], allow_extra_keys: bool = False) -> tuple[DataClass, ...]:
359 """
360 Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass
361 types.
362
363 Args:
364 args (`dict`):
365 dict containing config values
366 allow_extra_keys (`bool`, *optional*, defaults to `False`):
367 Defaults to False. If False, will raise an exception if the dict contains keys that are not parsed.
368
369 Returns:
370 Tuple consisting of:
371
372 - the dataclass instances in the same order as they were passed to the initializer.
373 """
374 unused_keys = set(args.keys())
375 outputs = []
376 for dtype in self.dataclass_types:
377 keys = {f.name for f in dataclasses.fields(dtype) if f.init}
378 inputs = {k: v for k, v in args.items() if k in keys}
379 unused_keys.difference_update(inputs.keys())
380 obj = dtype(**inputs)
381 outputs.append(obj)
382 if not allow_extra_keys and unused_keys:
383 raise ValueError(f"Some keys are not used by the HfArgumentParser: {sorted(unused_keys)}")
384 return tuple(outputs)
385
386 def parse_json_file(self, json_file: str | os.PathLike, allow_extra_keys: bool = False) -> tuple[DataClass, ...]:
387 """

Callers 3

parse_json_fileMethod · 0.95
parse_yaml_fileMethod · 0.95
test_09_parse_dictMethod · 0.95

Calls 2

keysMethod · 0.45
itemsMethod · 0.45

Tested by 1

test_09_parse_dictMethod · 0.76