Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

fix(cli): do not print the sparse-passenger-mix explanation twice in text mode (#256)

Punit Arani881aee5ff432
diff --git a/CLAUDE.md b/CLAUDE.md
index fd4cb2a..f52eafd 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -152,9 +152,11 @@ Consequences to keep in mind when changing search code:
came back with zero rows, not when the caller's own airline/price/
duration/window filter removed rows Google did return (see
`SearchFlights.sparse_passenger_mix`). The MCP `search_flights` empty
- response carries the same text as `note`, and the CLI prints it under
- "No flights found.", both reading that attribute rather than recomputing
- the condition — an adults-only search shows the schedule.
+ response and the CLI's `--format json` empty payload carry the same text
+ as `note`, both reading that attribute rather than recomputing the
+ condition. The CLI's text mode prints no copy of its own: the library's
+ warning already reaches the terminal on stderr, and echoing it showed the
+ same paragraph twice — an adults-only search shows the schedule.
- Date searches have no calendar grid: one page fetch per date, capped at
`fli.search.dates.MAX_DATES_PER_SEARCH` (93) per `SearchDates.search`. At the
cap that is several hundred MB of pages and parsed JSON at peak.
diff --git a/fli/cli/commands/flights.py b/fli/cli/commands/flights.py
index b84e4a7..ea42937 100644
--- a/fli/cli/commands/flights.py
+++ b/fli/cli/commands/flights.py
@@ -5,7 +5,6 @@ from typing import Annotated, Any
import typer
from pydantic import ValidationError
-from fli.cli.console import console
from fli.cli.enums import OutputFormat
from fli.cli.errors import json_error_payload, report_cli_error
from fli.cli.utils import (
@@ -250,9 +249,13 @@ def _search_flights_core(
)
return
+ # Text mode prints no copy of the note: SearchFlights.search has
+ # already logged the same explanation as a warning, which — like
+ # every other library warning — reaches the terminal on stderr.
+ # Echoing it here showed the user the same paragraph twice. JSON
+ # callers rarely read stderr, which is why the note rides in the
+ # payload above instead.
typer.echo("No flights found.")
- if sparse_note:
- console.print(sparse_note, style="dim", soft_wrap=True)
raise typer.Exit(1)
# Build per-flight booking deep-links (tfs; never raises).
diff --git a/tests/cli/test_flights.py b/tests/cli/test_flights.py
index 336398c..af4735b 100644
--- a/tests/cli/test_flights.py
+++ b/tests/cli/test_flights.py
@@ -1,6 +1,7 @@
"""Tests for the flights CLI command."""
import json
+import logging
from datetime import datetime, timedelta
import pytest
@@ -520,13 +521,17 @@ def test_flights_no_results(runner, mock_search_flights, mock_console):
assert "client-side" not in result.stdout
-def test_flights_no_results_with_child_explains_the_sparsity(runner, mock_search_flights):
- """An empty result for a party with children/infants gets the extra hint.
-
- Google's search page inlines fewer (sometimes zero) rows for those
- parties — see SPARSE_PASSENGER_MIX_WARNING in fli.search.flights. The
- CLI reads search_client.sparse_passenger_mix rather than recomputing
- it, so the mock sets it the way the real library would for this party.
+def test_flights_no_results_with_child_does_not_repeat_the_library_warning(
+ runner, mock_search_flights
+):
+ """Text mode leaves the explanation to the library's own warning.
+
+ Google's search page inlines fewer (sometimes zero) rows for parties with
+ children/infants — see SPARSE_PASSENGER_MIX_WARNING in fli.search.flights.
+ ``SearchFlights.search`` logs that warning itself, and like every other
+ library warning it reaches the terminal on stderr, so printing it again
+ on stdout would show the user the same paragraph twice. (The mock here
+ logs nothing, which is what isolates the CLI's own output.)
"""
mock_search_flights.search.return_value = []
mock_search_flights.sparse_passenger_mix = True
@@ -546,10 +551,12 @@ def test_flights_no_results_with_child_explains_the_sparsity(runner, mock_search
)
assert result.exit_code == 1
assert "No flights found" in result.stdout
- assert "client-side" in result.stdout
+ assert "client-side" not in result.stdout
-def test_flights_no_results_with_infant_explains_the_sparsity(runner, mock_search_flights):
+def test_flights_no_results_with_infant_does_not_repeat_the_library_warning(
+ runner, mock_search_flights
+):
mock_search_flights.search.return_value = []
mock_search_flights.sparse_passenger_mix = True
@@ -565,17 +572,26 @@ def test_flights_no_results_with_infant_explains_the_sparsity(runner, mock_searc
],
)
assert result.exit_code == 1
- assert "client-side" in result.stdout
+ assert "client-side" not in result.stdout
class TestFlightsNoteTracksGoogleNotTheCallersFilter:
"""End-to-end (stubbed page, real ``SearchFlights.search``) — mirrors the MCP-level check.
- A page that genuinely carries no rows still gets the hint; a page that
- carries a row the caller's own airline filter then removes does not —
- that emptiness is the filter's doing, not Google's.
+ A page that genuinely carries no rows is explained exactly once — by the
+ library's warning, not repeated on stdout; a page that carries a row the
+ caller's own airline filter then removes is not explained at all — that
+ emptiness is the filter's doing, not Google's.
"""
+ @staticmethod
+ def _sparse_warnings(caplog) -> list[str]:
+ return [
+ record.getMessage()
+ for record in caplog.records
+ if record.levelno == logging.WARNING and "client-side" in record.getMessage()
+ ]
+
def _page(self, rows: list) -> str:
payload = [[None, None, None, None, "FAKE_SESSION"], None, [rows], None]
return as_search_page(payload)
@@ -586,20 +602,32 @@ class TestFlightsNoteTracksGoogleNotTheCallersFilter:
monkeypatch.setattr("fli.search.client.Client.get", _fake_get)
- def test_genuinely_empty_page_prints_the_hint(self, runner, monkeypatch):
+ def test_genuinely_empty_page_is_explained_exactly_once(self, runner, monkeypatch, caplog):
self._stub_get(monkeypatch, self._page([]))
- result = runner.invoke(
- app,
- ["flights", "JFK", "LHR", datetime.now().strftime("%Y-%m-%d"), "--children", "1"],
- )
+ with caplog.at_level(logging.WARNING, logger="fli"):
+ result = runner.invoke(
+ app,
+ ["flights", "JFK", "LHR", datetime.now().strftime("%Y-%m-%d"), "--children", "1"],
+ )
assert result.exit_code == 1
assert "No flights found" in result.stdout
- assert "client-side" in result.stdout
+ # Once, from the library (stderr in a real terminal) — never echoed on stdout too.
+ assert len(self._sparse_warnings(caplog)) == 1
+ assert "client-side" not in result.stdout
- def test_rows_filtered_out_by_airline_prints_no_hint(self, runner, monkeypatch):
+ def test_rows_filtered_out_by_airline_is_not_explained(self, runner, monkeypatch, caplog):
row = _row(legs=[_leg(dep_iata="JFK", arr_iata="LHR", airline_code="DL")])
self._stub_get(monkeypatch, self._page([row]))
- result = runner.invoke(
+ with caplog.at_level(logging.WARNING, logger="fli"):
+ result = self._invoke_filtered(runner)
+ assert result.exit_code == 1
+ assert "No flights found" in result.stdout
+ assert self._sparse_warnings(caplog) == []
+ assert "client-side" not in result.stdout
+
+ @staticmethod
+ def _invoke_filtered(runner):
+ return runner.invoke(
app,
[
"flights",
@@ -612,9 +640,6 @@ class TestFlightsNoteTracksGoogleNotTheCallersFilter:
"AA",
],
)
- assert result.exit_code == 1
- assert "No flights found" in result.stdout
- assert "client-side" not in result.stdout
def test_basic_round_trip_flights(runner, mock_search_flights, mock_console):