agnes-the-ai-analyst/cli/commands/describe.py
ZdenekSrotyr 751cc25327
release: 0.46.5 — agnes describe -n parses, server sanitizes NaN (#224)
## Summary

Two bugs in `agnes describe` surfaced from a real analyst session following the CLAUDE.md agent-rails discovery workflow. Together they break `agnes describe` end-to-end for any analyst (or analyst-AI) who follows the documented form.

### A) CLI parsing

`agnes describe TABLE -n 5` failed with `Missing argument 'TABLE_ID'`. Root cause: the command was registered as a `Typer.Typer` subcommand group via `app.add_typer(describe_app, name="describe")` + `@describe_app.callback(invoke_without_command=True)`, and that pattern mis-parses positional + short-int option in some orderings. Same pattern in `cli/commands/schema.py` works only because schema has no INTEGER short option. Fix: switch to flat `@app.command("describe")`.

### B) Server NaN

`/api/v2/sample/<id>` (called by `agnes describe`) returned HTTP 500 with `ValueError: Out of range float values are not JSON compliant: nan` whenever a row contained NaN. Fix: sanitize NaN/±inf to None before JSON serialization.

## Test plan

- [x] `pytest tests/test_cli_describe*.py` — added regression tests pinning `-n` parsing on either side of the positional.
- [x] `pytest tests/test_api_v2_sample*.py` — added regression test for NaN row → JSON `null` (not 500).
<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/keboola/agnes-the-ai-analyst/pull/224" target="_blank">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1">
    <img src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1" alt="Open in Devin Review">
  </picture>
</a>
<!-- devin-review-badge-end -->
2026-05-07 18:16:21 +02:00

42 lines
1.6 KiB
Python

"""`agnes describe <table>` — schema + sample rows (spec §4.1).
Registered as a flat ``@app.command("describe")`` in ``cli/main.py`` rather
than as a ``Typer.Typer`` subcommand-group + callback. The group pattern
mis-parses ``agnes describe TABLE -n 5`` (positional + short option with a
separated INTEGER value) — Typer hands the "5" to the positional and then
errors on a missing TABLE_ID. There were no actual subcommands of
``describe`` to justify the group wrapping anyway. Issue surfaced from a
real analyst session following the CLAUDE.md "agent rails" workflow.
"""
import json as json_lib
import typer
from cli.v2_client import api_get_json, V2ClientError
def describe(
table_id: str = typer.Argument(...),
n: int = typer.Option(5, "-n", "--rows", help="Sample rows count"),
json: bool = typer.Option(False, "--json"),
):
"""Show schema + sample rows for a table."""
try:
sch = api_get_json(f"/api/v2/schema/{table_id}")
sam = api_get_json(f"/api/v2/sample/{table_id}", n=n)
except V2ClientError as e:
typer.echo(f"Error: describe failed: {e}", err=True)
raise typer.Exit(5 if e.status_code >= 500 else 8 if e.status_code == 403 else 2)
if json:
typer.echo(json_lib.dumps({"schema": sch, "sample": sam}, indent=2, default=str))
return
typer.echo(f"Table: {sch['table_id']}")
typer.echo("")
typer.echo("Schema:")
for c in sch.get("columns", []):
typer.echo(f" {c['name']:30s} {c['type']}")
typer.echo("")
typer.echo(f"Sample ({len(sam.get('rows', []))} rows):")
for row in sam.get("rows", []):
typer.echo(f" {row}")