diff --git a/changelog.d/1597-qa-duplicates-threshold.fixed.md b/changelog.d/1597-qa-duplicates-threshold.fixed.md new file mode 100644 index 000000000..8d52476ad --- /dev/null +++ b/changelog.d/1597-qa-duplicates-threshold.fixed.md @@ -0,0 +1 @@ +- **Every Pyrite command starts again on Typer 0.29 (#1597).** `qa duplicates --threshold` used a `click_type=` setting Typer 0.29 removed, so no command line could even start. The option now parses the value with the same 0-to-1 check (NaN refused), and `--threshold 2`, `nan` and `abc` are still refused as usage errors. diff --git a/pyrite/cli/qa_commands.py b/pyrite/cli/qa_commands.py index bdfefd09f..f1f8e6837 100644 --- a/pyrite/cli/qa_commands.py +++ b/pyrite/cli/qa_commands.py @@ -4,7 +4,6 @@ Commands: validate, duplicates, status, assess """ -import click import typer from rich.console import Console from rich.table import Table @@ -16,24 +15,21 @@ from .output import OUTPUT_FORMAT, cli_console, output_format_choice, validate_output_format -class _UnitInterval(click.ParamType): +def _parse_unit_interval(value: str) -> float: """A number from 0 to 1, NaN refused. ``min=0, max=1`` lets NaN through (it compares false with both bounds), so the range is checked here, at parse time, where a refusal is the usage error (exit 2) a bad range already is. A - parameter *callback* is forbidden on the Outcome contract; a type is not. - The error is ``typer.BadParameter``: Typer 0.27 vendors click, and only its - own class is answered as ``USAGE_ERROR`` (exit 2).""" + parameter *callback* is forbidden on the Outcome contract; a parser is not. + The error is ``typer.BadParameter``: Typer vendors click, and only its own + class is answered as ``USAGE_ERROR`` (exit 2).""" - name = "float in [0, 1]" - - def convert(self, value, param, ctx): - try: - number = float(value) - except (TypeError, ValueError): - raise typer.BadParameter(f"{value!r} is not a valid number", ctx=ctx, param=param) - if not 0.0 <= number <= 1.0: - raise typer.BadParameter(f"{value!r} is not in the range 0 to 1", ctx=ctx, param=param) - return number + try: + number = float(value) + except (TypeError, ValueError): + raise typer.BadParameter(f"{value!r} is not a valid number") + if not 0.0 <= number <= 1.0: + raise typer.BadParameter(f"{value!r} is not in the range 0 to 1") + return number qa_app = typer.Typer(help="Quality assurance validation and assessment") @@ -158,7 +154,9 @@ def qa_duplicates( threshold: float = typer.Option( 0.62, "--threshold", - click_type=_UnitInterval(), + parser=_parse_unit_interval, + min=0, + max=1, help="Minimum score; a pair at or above it is a candidate", ), strip_prefix: list[str] | None = typer.Option( diff --git a/tests/test_cli_numeric_options_declare_a_range.py b/tests/test_cli_numeric_options_declare_a_range.py index ee6171b21..3c3aaa2b2 100644 --- a/tests/test_cli_numeric_options_declare_a_range.py +++ b/tests/test_cli_numeric_options_declare_a_range.py @@ -255,7 +255,7 @@ def registry() -> dict[str, object]: #: ``parser=`` or ``click_type=`` option) and why that is safe. One row: `qa duplicates --threshold`. UNCLASSIFIED: dict[tuple[str, str], str] = { ("pyrite qa duplicates", "threshold"): ( - "a click float range accepts NaN; this hand-built type refuses a non-finite or " + "a click float range accepts NaN; this parser refuses a non-finite or " "out-of-range value at parse time (usage error, exit 2); to be replaced by the " "shared finite-float type tracked on #1541" ), diff --git a/tests/test_qa_duplicates.py b/tests/test_qa_duplicates.py index 31cd0051a..5147ef6a9 100644 --- a/tests/test_qa_duplicates.py +++ b/tests/test_qa_duplicates.py @@ -6,6 +6,7 @@ import time import pytest +import typer from pyrite.config import KBConfig, KBType, PyriteConfig, Settings from pyrite.models.task import TaskEntry @@ -125,14 +126,28 @@ def test_cli_rich_format_prints_one_line_per_pair(self, cli_world): assert "~" in result.stdout assert "[title]" in result.stdout - def test_a_bad_threshold_is_a_usage_error(self, cli_world): + @pytest.mark.parametrize("value", ["2", "abc"]) + def test_a_bad_threshold_is_a_usage_error(self, cli_world, value): world = cli_world() - result = world.cli("qa", "duplicates", "-t", "task", "--threshold", "2") + result = world.cli("qa", "duplicates", "-t", "task", "--threshold", value) assert result.exit_code == 2 assert "threshold" in (result.output + getattr(result, "stderr", "")).lower() + @pytest.mark.parametrize("value", ["0", "1"]) + def test_cli_accepts_a_threshold_on_the_boundary(self, cli_world, value): + world = _world_with_pair(cli_world) + + result = world.cli( + "qa", "duplicates", "-t", "task", "--threshold", value, "--format", "json" + ) + + assert result.exit_code == 0, result.output + payload = json.loads(result.stdout) + assert payload["threshold"] == float(value) + assert payload["count"] == 1 + def test_two_thousand_entries_stay_inside_the_bound(self, dup_setup): rows = [ (f"ticket-{index:04d}", "dups", "task", f"Ticket {index:04d}", "open", "active") @@ -366,6 +381,21 @@ def test_cli_refuses_a_non_finite_threshold_as_a_usage_error(cli_world, value): assert "NO_OUTCOME" not in result.stdout + result.output +@pytest.mark.parametrize("value", ["2", "nan", "inf", "-inf", "abc", "", "1.1", "-0.1"]) +def test_parse_unit_interval_refuses_bad_values(value): + from pyrite.cli.qa_commands import _parse_unit_interval + + with pytest.raises(typer.BadParameter, match="not a valid number|not in the range"): + _parse_unit_interval(value) + + +@pytest.mark.parametrize(("value", "expected"), [("0.5", 0.5), ("0", 0.0), ("1", 1.0)]) +def test_parse_unit_interval_accepts_values_in_range(value, expected): + from pyrite.cli.qa_commands import _parse_unit_interval + + assert _parse_unit_interval(value) == expected + + @pytest.mark.parametrize("value", [float("nan"), float("inf"), -0.1, 1.1]) def test_service_refuses_a_threshold_outside_zero_to_one(dup_setup, value): from pyrite.exceptions import ValidationError