Repository navigation
Conversation
| if isinstance(v, dict): | ||
| return v | ||
| try: | ||
| val = json.loads(v) | ||
| if isinstance(val, dict): | ||
| return val | ||
| raise ArgumentTypeError(f"Expected a dict/JSON object, but got {type(val).__name__}: {v!r}") | ||
| except (json.JSONDecodeError, TypeError): | ||
| pass | ||
|
|
||
| try: |
There was a problem hiding this comment.
Good approach! Two small simplifications for inline JSON:
- Preserve JSON syntax feedback. If a user passes inline JSON with a typo (e.g. missing quote or unclosed brace), swallowing JSONDecodeError falls through to the file check and gives a generic error. If the trimmed input starts with {, letting JSONDecodeError surface directly gives the user exact line/column feedback on their typo.
- Guard against non-strings. If v is None or a non-string, os.path.isfile later raises an unhandled TypeError. Adding an early if not isinstance(v, str): check keeps this safe.
There was a problem hiding this comment.
Done. Added non-string guard and let JSONDecodeError surface directly on trimmed { inputs.
| if os.path.isfile(v): | ||
| try: | ||
| with open(v, "r", encoding="utf-8") as f: | ||
| val = json.load(f) | ||
| if isinstance(val, dict): | ||
| return val | ||
| raise ArgumentTypeError(f"Expected a dict from file '{v}', but got {type(val).__name__}") | ||
| except Exception as e: | ||
| raise ArgumentTypeError(f"Failed to load JSON file '{v}': {e}") | ||
| except OSError: | ||
| pass |
There was a problem hiding this comment.
Two cleanups on the file-loading logic
- Python's stdlib, os.path.isfile() already catches OSError internally and returns False, so the outer try...except OSError block can be safely eliminated.
- When a JSON file contains a list instead of a dict, line 67 raises ArgumentTypeError. The except Exception as e: below it catches that same exception and re-wraps it, creating a double-nested error message ("Failed to load JSON file '...': Expected a dict from file..."). Catching specifically (json.JSONDecodeError, OSError) avoids this and lets ArgumentTypeError bubble up cleanly.
There was a problem hiding this comment.
Done. Removed redundant outer OSError block and narrowed file load catch to (json.JSONDecodeError, OSError).
|
cc @SunMarc maybe? |
|
Applied the feedback @nandan2003. Trimmed inline inputs starting with { now surface JSON errors directly, added the non-string type guard, and narrowed the file loading exception to avoid double-nested errors. Pushed and tests passing! |
CI recapDashboard: View test results in Grafana |
|
@SunMarc gentle ping when you have a moment — all feedback from @nandan2003 was addressed with tests in place. ready for final review. |
Fixes #48030
HfArgumentParserfails when parsingdict-typed dataclass fields from CLI arguments because_parse_dataclass_fieldfalls through to the generic type constructor, raising an invalid dict value error.This adds a
string_to_dictparser supporting JSON strings and file paths, matching the existing list-handling pattern in_parse_dataclass_field.