Skip to content
Closed
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
1 change: 1 addition & 0 deletions changelog.d/1597-qa-duplicates-threshold.fixed.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 14 additions & 16 deletions pyrite/cli/qa_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Commands: validate, duplicates, status, assess
"""

import click
import typer
from rich.console import Console
from rich.table import Table
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli_numeric_options_declare_a_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
Expand Down
34 changes: 32 additions & 2 deletions tests/test_qa_duplicates.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""`qa duplicates` reports candidate near-duplicate entries, never merges (#934)."""

from __future__ import annotations
Expand All @@ -6,6 +6,7 @@
import time

import pytest
import typer

from pyrite.config import KBConfig, KBType, PyriteConfig, Settings
from pyrite.models.task import TaskEntry
Expand Down Expand Up @@ -125,14 +126,28 @@
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")
Expand Down Expand Up @@ -366,6 +381,21 @@
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
Expand Down
Loading