Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- **`salli-server` runs with Typer 0.29.** Where Typer 0.29 was installed, as a pip install of the package gets it, every command failed with a `TypeError`: the `--json` flag was built by calling Typer's option class, whose arguments 0.29 changed. Typer now builds the flag from a declaration, as it builds every other option; tested with Typer 0.26.7 to 0.29.0.

## 0.6.2 — 2026-11-10

The console's Cash flow by month draws the twelve months Home averages over, and the console is leaner on a phone.
Expand Down
39 changes: 28 additions & 11 deletions src/salli/interfaces/cli/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import typer
from rich.console import Console
from typer.core import TyperOption

console = Console()

Expand Down Expand Up @@ -103,21 +102,39 @@ def leaf_commands(group: Any, path: tuple[str, ...] = ()) -> Iterator[tuple[tupl
yield path, group


def _json_flag() -> Any:
"""A new --json option for one command. Typer builds it from a declaration,
as it builds every other option, so nothing here depends on the arguments
of Typer's option class (0.29 added a required one to TyperOption)."""

def declare(
json: bool | None = typer.Option(
None,
"--json",
is_eager=True,
expose_value=False,
# typer.Option shows both by default; --json shows neither.
show_default=False,
show_envvar=False,
help="Print the result as JSON on stdout (messages go to stderr).",
),
) -> None:
"""Never run: only its option is used."""

probe = typer.Typer(add_completion=False)
probe.command()(declare)
[flag] = typer.main.get_command(probe).params
# Set here rather than passed to typer.Option, which would wrap it.
flag.callback = _enable_json
return flag


def with_json_option(root: Any) -> Any:
"""Give every non-interactive command a --json flag."""
for path, command in leaf_commands(root):
if path in INTERACTIVE_COMMANDS or any(p.name == "json" for p in command.params):
continue
command.params.append(
TyperOption(
param_decls=["--json"],
is_flag=True,
expose_value=False,
is_eager=True,
callback=_enable_json,
help="Print the result as JSON on stdout (messages go to stderr).",
)
)
command.params.append(_json_flag())
return root


Expand Down
17 changes: 17 additions & 0 deletions tests/unit/cli/test_server_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import json
import re
import sys
import tomllib
from pathlib import Path
Expand Down Expand Up @@ -93,6 +94,22 @@ def test_every_command_takes_json():
), path


def test_the_json_flag_is_eager_unexposed_and_in_the_help(monkeypatch, capsys):
# Typer builds it from a declaration (support._json_flag): constructing
# TyperOption directly broke with typer 0.29, which added an argument.
for path, command in leaf_commands(main.cli()):
[flag] = [p for p in command.params if p.name == "json"]
assert (flag.opts, flag.secondary_opts) == (["--json"], []), path
assert flag.is_flag and flag.is_eager and not flag.expose_value, path
assert flag.callback is support._enable_json, path
assert flag.help == "Print the result as JSON on stdout (messages go to stderr).", path
helped = _run(monkeypatch, capsys, "db", "current", "--help")
assert helped.exit_code == 0, helped.output
# Typer colours the help where GITHUB_ACTIONS or FORCE_COLOR is set.
shown = re.sub(r"\x1b\[[0-9;]*m", "", helped.output)
assert "--json" in shown and "Print the result as JSON" in shown


def test_the_names_extensions_import_stay_put():
# A deployment's own commands and tests import these; renaming one breaks it.
for name in ("console", "emit", "json_mode", "require_user", "services", "leaf_commands"):
Expand Down
Loading