Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

Fix IndexError in Flight Parsing (#26)

Punit Aranid2d446aa4dde
diff --git a/fli/search/flights.py b/fli/search/flights.py
index c45e6cb..0745016 100644
--- a/fli/search/flights.py
+++ b/fli/search/flights.py
@@ -110,7 +110,7 @@ class SearchFlights:
"""
flight = FlightResult(
- price=data[1][0][-1],
+ price=SearchFlights._parse_price(data),
duration=data[0][9],
stops=len(data[0][2]) - 1,
legs=[
@@ -128,6 +128,24 @@ class SearchFlights:
)
return flight
+ @staticmethod
+ def _parse_price(data: list) -> float:
+ """Extract price from raw flight data.
+
+ Args:
+ data: Raw flight data from the API response
+
+ Returns:
+ Flight price, or 0.0 if price data is unavailable
+
+ """
+ try:
+ if data[1] and data[1][0]:
+ return data[1][0][-1]
+ except (IndexError, TypeError):
+ pass
+ return 0.0
+
@staticmethod
def _parse_datetime(date_arr: list[int], time_arr: list[int]) -> datetime:
"""Convert date and time arrays to datetime.
diff --git a/tests/search/test_search_flights.py b/tests/search/test_search_flights.py
index ed59afd..7a8cabd 100644
--- a/tests/search/test_search_flights.py
+++ b/tests/search/test_search_flights.py
@@ -251,3 +251,37 @@ def test_round_trip_result_structure(search, search_params_fixture, request):
assert hasattr(flight, "stops")
assert hasattr(flight, "legs")
assert len(flight.legs) > 0
+
+
+class TestParsePrice:
+ """Tests for _parse_price method handling missing/malformed price data."""
+
+ def test_parse_price_valid_data(self):
+ """Test _parse_price with valid price data."""
+ data = [None, [[100, 200, 299.99]]]
+ assert SearchFlights._parse_price(data) == 299.99
+
+ def test_parse_price_empty_inner_list(self):
+ """Test _parse_price returns 0.0 when inner price list is empty."""
+ data = [None, [[]]]
+ assert SearchFlights._parse_price(data) == 0.0
+
+ def test_parse_price_empty_outer_list(self):
+ """Test _parse_price returns 0.0 when outer price list is empty."""
+ data = [None, []]
+ assert SearchFlights._parse_price(data) == 0.0
+
+ def test_parse_price_none_price_section(self):
+ """Test _parse_price returns 0.0 when price section is None."""
+ data = [None, None]
+ assert SearchFlights._parse_price(data) == 0.0
+
+ def test_parse_price_missing_price_section(self):
+ """Test _parse_price returns 0.0 when data has no price section."""
+ data = [None]
+ assert SearchFlights._parse_price(data) == 0.0
+
+ def test_parse_price_inner_list_none(self):
+ """Test _parse_price returns 0.0 when inner list is None."""
+ data = [None, [None]]
+ assert SearchFlights._parse_price(data) == 0.0