FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(cli): add --json output to pn doctor by shramanb113 · Pull Request #76 · pythonnative/pythonnative · GitHub

feat(cli): add --json output to pn doctor - #76

Merged
owenthcarey merged 2 commits into
pythonnative:mainfrom
shramanb113:feat/doctor-json
Sep 8, 2026
Merged

owenthcarey merged 2 commits into
pythonnative:mainfrom
shramanb113:feat/doctor-json

Conversation

Copy link
Copy Markdown
Contributor

What

pn doctor gains a --json flag for scripting, mirroring pn devices --json (#22). With --json, stdout carries a JSON array of check results and nothing else; the human verdict line moves to stderr. Exit codes are unchanged — still keyed to the worst check level.

Nothing changes for the default invocation: pn doctor without the flag prints the same report it always has.

Why

  • Tooling and CI that want to gate on toolchain health had to parse formatted, alignment-padded text. pn devices already solved this with --json; pn doctor should match.
  • Keeping stdout pure JSON and pushing the verdict to stderr means pn doctor --json | jq works without a filter step, the same contract pn devices --json uses.

How (brief)

src/pythonnative/project/doctor.py

  • Add CheckResult.to_dict() returning {"name", "level", "message"}. The dataclass field is detail; it's exposed as message in the payload since that reads better for a consumer and is what the issue asked for.

src/pythonnative/cli/pn.py

  • Add --json to the doctor subparser (action="store_true"), wording modelled on the devices parser.
  • doctor_command computes worst_level once, then branches: --json prints json.dumps([r.to_dict() for r in results], indent=2) to stdout and the verdict to stderr; the report path is unchanged except the verdict now goes through the shared helper. sys.exit(1) on ERROR is a single tail statement for both paths.
  • Extract _print_doctor_summary(level, stream) so the three verdict strings can't drift between the report and --json, the same pattern as _print_no_devices_hints(stream) for pn devices.
  • Update the module docstring's pn doctor line.

tests/test_cli.py

  • Four tests following the pn devices --json tests: JSON array parses with keys {name, level, message}; report header/verdict kept off stdout with the verdict on stderr; ERROR still exits 1 with the verdict on stderr; --json wired through argparse via a real pn init project driven by run_pn.

tests/project/test_doctor.py

  • Unit test for CheckResult.to_dict(), including the empty-detail default.

docs/api/cli.md

  • pn doctor bullet documents --json: JSON array to stdout, verdict to stderr, exit status unchanged.

Testing

  • ./scripts/check.sh passes (ruff, black, mypy, pytest, e2e coverage).
  • New doctor tests: 17 pass (13 in test_doctor.py, 4 in test_cli.py).
  • The 4 test_cli_init_rejects_symlinked_target failures on Windows are pre-existing (symlink creation needs admin privilege) and reproduce identically on main — unrelated to this change.

Risks/Impact

  • Default pn doctor output and exit codes are unchanged; only the new flag adds behaviour.
  • --json verdict text is identical to the report's, routed to stderr — safe for | jq and for scripts that only inspect the exit code.
  • No dependency or API changes.

Docs/Follow-ups

  • CLI reference (docs/api/cli.md) and the pn.py module docstring updated in this PR.

Closes #37

mkdocs --strict build was failing because faq.md and
troubleshooting.md link to testing.md#a-minimal-fake-backend, but
no section with that anchor existed.
owenthcarey merged commit 375c3a2 into pythonnative:main Sep 8, 2026
18 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --json output to pn doctor for scripting

2 participants


Back | FazBrowse Home | New Git URL