Skip to content

Change default parser mode to json_or_yaml and say which format failed in parse errors - #1004

Merged
mauvilsa merged 2 commits into
mainfrom
json-or-yaml-parser-mode
Oct 6, 2026
Merged

mauvilsa merged 2 commits into
mainfrom
json-or-yaml-parser-mode

Conversation

@mauvilsa

@mauvilsa mauvilsa commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

What does this PR do?

Making json the default parser_mode (#997) turned out to be very disruptive, since YAML configs and values like --list=[a, b] stopped working. This PR replaces it with a less disruptive default:

  • New json_or_yaml parser mode, now the default. It parses as JSON and, only if that fails and pyyaml is installed, as YAML. Valid JSON parses the same whether or not pyyaml is installed, and anything that worked with the v4 yaml default keeps working when pyyaml is installed. Dumps, e.g. --print_config, are YAML when pyyaml is installed, otherwise JSON.
  • Parse errors now say which format failed, e.g. Failed to parse as JSON: .... In json mode, a value that parses as a YAML mapping or list gets a hint to use parser_mode='json_or_yaml'. Previously a YAML config given in json mode only showed a misleading Expecting value: line 1 column 1.
  • Removed the parser_mode setting of set_parsing_settings and the JSONARGPARSE_PARSER_MODE environment variable (both unreleased). They were added to undo the strict json default, and with json_or_yaml they are no longer needed.
  • The jsonnet parser mode accepts YAML again, as in v4, but only for Jsonnet syntax errors, so evaluation errors are no longer hidden by a YAML parse.
  • Fixed the docs of JSONARGPARSE_DEFAULT_ENV and JSONARGPARSE_ALLOW_ABBREV to say how they interact with parsers given default_env or allow_abbrev explicitly.

Before submitting

  • Did you read the contributing guideline?
  • If you used a coding agent, did you fully understand and validate all generated code and ensure it follows the contributing guidelines?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • [n/a] If this is a bug fix, did you verify that the tests fail without the code fix?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG including a pull request link? (not for typos, docs, test updates, or minor internal changes/refactors)

@mauvilsa mauvilsa added enhancement New feature or request refactor Improvements to the quality of the code labels Oct 6, 2026
Comment thread jsonargparse_tests/test_loaders_dumpers.py Fixed
@mauvilsa
mauvilsa deployed to sonarcloud October 6, 2026 05:23 — with GitHub Actions Active
@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5070670) to head (1ad358d).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1004   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         9681      9751   +70     
=========================================
+ Hits          9681      9751   +70     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 2/5

[Medium risk] Changes default config file parser mode and error messages.

The PR is not ready to merge because three reachable paths can mask errors or silently accept incorrect values.

Findings

  1. P1 Valid JSON masks validation errors ▶
  2. P1 Jsonnet errors are suppressed ▶
  3. P1 Malformed values are silently accepted ▶

Summary

The PR makes json_or_yaml the default parser mode, adds format-specific parse errors, restores YAML fallback in Jsonnet mode, and updates dump behavior and documentation.

  • JSON is tried before optional YAML parsing; default dumps use YAML when PyYAML is installed.
  • Three error-handling paths need correction: JSON provenance lookup, Jsonnet evaluation fallback, and malformed unvalidated typed values.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Config or argument value] --> B{Parser mode}
  B -->|json_or_yaml| C[Try JSON]
  C -->|fails; PyYAML installed| D[Try YAML]
  B -->|jsonnet| E[Evaluate Jsonnet]
  E -->|RuntimeError| D
  C -->|succeeds| F[Validate value]
  D -->|succeeds| F
  F -->|validation error with config source| G[Look up source position]
  G --> H[Compose file as YAML]
Loading

Reviews (1) · Last reviewed commit: "Change default parser mode to json_or_ya..."

Comment thread jsonargparse/_formatters.py
Comment thread jsonargparse/_loaders_dumpers.py Outdated
Comment thread jsonargparse/_typehints.py
@mauvilsa
mauvilsa deployed to sonarcloud October 6, 2026 05:49 — with GitHub Actions Active
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit 71a8755 into main Oct 6, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the json-or-yaml-parser-mode branch October 6, 2026 05:51

This branch was successfully deployed

1 active deployment
sonarcloud — 1ad358d0 Deployed Oct 6, 2026 by mauvilsa via sonarcloud #948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request refactor Improvements to the quality of the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants