Skip to content

fix: use removeprefix and fix remaining underscore-prefix serialization paths#94

Open
ktrufanov-skycop wants to merge 1 commit intopunitarani:mainfrom
ktrufanov-skycop:main
Open

fix: use removeprefix and fix remaining underscore-prefix serialization paths#94
ktrufanov-skycop wants to merge 1 commit intopunitarani:mainfrom
ktrufanov-skycop:main

Conversation

@ktrufanov-skycop
Copy link
Copy Markdown
Contributor

@ktrufanov-skycop ktrufanov-skycop commented Mar 30, 2026

Summary

Follow-up to #90 (merged). Addresses the serialization gap flagged
by @punitarani in the review comment
digit-prefixed airline codes like _3F were still leaking into API
requests and CLI output through paths not covered by the first PR.

Changes

  • removeprefix("_") in flights.py and dates.py serialize()
    functions — strip the synthetic underscore before sending codes to
    Google Flights API
  • serialize_airline() in cli/utils.py — same fix for CLI JSON output
  • Round-trip selected_flight in flights.py — pass enum objects
    (leg.airline, leg.departure_airport, leg.arrival_airport) to
    serialize() instead of pre-resolving .name, which bypassed the
    isinstance check and leaked _3F into the request payload
  • Test for serialize_airline() with numeric-prefix code

Why removeprefix over lstrip

lstrip("_") would strip all leading underscores (__3F3F).
removeprefix("_") removes exactly one — matching the single underscore
added by generate_enums.py for digit-starting IATA codes.

Test plan

  • serialize_airline(Airline._3F) returns {"code": "3F", "name": "FlyOne Armenia"}
  • All 6 airline-related tests pass
  • No regressions in existing tests

Greptile Summary

This follow-up PR closes the remaining serialization gaps from #90, ensuring digit-prefixed airline codes like _3F are stripped to their canonical IATA form (3F) across all code paths: Google Flights API requests (flights.py, dates.py serialize functions), CLI JSON output (cli/utils.py serialize_airline), and input parsing (core/parsers.py parse_airlines). It also fixes a more subtle round-trip bug where selected_flight legs were passing .name strings into serialize(), bypassing the isinstance(obj, Airline) guard entirely.

Key changes:

  • lstrip(\"_\")removeprefix(\"_\") in cli/utils.py and both serialize() functions — prevents over-stripping if a code somehow contained multiple leading underscores
  • dates.py serialize() — was the missed path from fix: correct stale airline data for 3F and fix numeric-prefix airline filtering #90; now consistent with flights.py
  • flights.py round-trip selected_flight — now passes leg.departure_airport, leg.arrival_airport, and leg.airline enum objects (not .name strings) so the isinstance guard applies correctly
  • parsers.py parse_airlines — correctly prefixes digit-leading codes with _ before enum lookup
  • data/airlines.csv and Airline._3F enum — updated from "Pacific Airways" to "FlyOne Armenia"
  • Two new tests covering the numeric-prefix round-trip

One gap remains: fli/cli/commands/dates.py line 234 builds the query echo with airline.name (post-parse enum names), so \"_3F\" still leaks into the query.airlines field of the JSON output for the dates command.

Confidence Score: 4/5

Safe to merge with the minor caveat that the dates command's JSON query echo still leaks the underscore prefix for digit-prefixed airlines.

All API serialization paths and CLI result output are correctly fixed. The one remaining defect is in the informational query-echo field of the dates command JSON output (fli/cli/commands/dates.py:234), which was explicitly part of the PR's stated goal ("CLI output" leaks) but was missed. This is a real, user-visible inconsistency rather than a pure style issue, warranting a 4 rather than 5.

fli/cli/commands/dates.py — line 234 still uses airline.name without removeprefix("_") in the query echo dict.

Important Files Changed

Filename Overview
fli/models/google_flights/flights.py Fixed serialize() to call removeprefix("_") on Airline/Airport names, and fixed selected_flight round-trip serialization to pass enum objects instead of raw .name strings — correctly routes through the isinstance guard.
fli/models/google_flights/dates.py Fixed the previously missed serialize() function to apply removeprefix("_"), closing the gap for date search API requests with digit-prefixed airline codes.
fli/core/parsers.py parse_airlines now correctly maps user-supplied codes like "3F" to "_3F" enum keys before lookup, enabling round-trip input→enum→output without leaking the underscore.
fli/cli/utils.py serialize_airline now strips the underscore prefix via removeprefix("_"), fixing JSON CLI output for digit-prefixed airline codes.
tests/cli/test_utils.py Added test_parse_airlines_numeric_prefix and test_serialize_airline_numeric_prefix covering the two fixed code paths; assertions match the updated airline data.
fli/models/airline.py _3F value updated from "Pacific Airways" to "FlyOne Armenia" to match the airlines.csv data correction.
data/airlines.csv 3F airline name corrected from "Pacific Airways" to "FlyOne Armenia".

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    UserInput["User input: '3F'"] --> ParseAirlines["parse_airlines()\ncore/parsers.py\ncode[0].isdigit() → prepend '_'"]
    ParseAirlines --> EnumLookup["getattr(Airline, '_3F')\n→ Airline._3F"]
    EnumLookup --> TwoPaths{Output path}

    TwoPaths -->|API request| Serialize["serialize(obj)\nflights.py / dates.py\nobj.name.removeprefix('_')\n→ '3F' ✅"]
    TwoPaths -->|CLI JSON results| SerializeAirline["serialize_airline()\ncli/utils.py\nairline.name.removeprefix('_')\n→ '3F' ✅"]
    TwoPaths -->|CLI JSON query echo\ndates command| QueryEcho["airline.name\ncli/commands/dates.py:234\n→ '_3F' ❌ still leaks"]

    Serialize --> APIRequest["Google Flights API\nrequest payload"]
    SerializeAirline --> CLIOutput["CLI / MCP\nflight results"]
    QueryEcho --> QueryField["query.airlines field\nin JSON response"]
Loading

Comments Outside Diff (1)

  1. fli/cli/commands/dates.py, line 234 (link)

    P1 Underscore prefix still leaks in query echo

    This line serializes parsed Airline enum names directly into the JSON query field. For digit-prefixed codes like Airline._3F, airline.name returns "_3F" rather than the user-supplied "3F". This is one of the serialization paths the PR description explicitly targets ("digit-prefixed airline codes … still leaking into … CLI output"), but it was missed here.

    The flights command works correctly because it echoes the raw input strings before parsing. The dates command echoes the post-parsed Airline.name, so the prefix leaks.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: fli/cli/commands/dates.py
    Line: 234
    
    Comment:
    **Underscore prefix still leaks in query echo**
    
    This line serializes parsed `Airline` enum names directly into the JSON `query` field. For digit-prefixed codes like `Airline._3F`, `airline.name` returns `"_3F"` rather than the user-supplied `"3F"`. This is one of the serialization paths the PR description explicitly targets ("digit-prefixed airline codes … still leaking into … CLI output"), but it was missed here.
    
    The `flights` command works correctly because it echoes the raw input strings before parsing. The `dates` command echoes the post-parsed `Airline.name`, so the prefix leaks.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: fli/cli/commands/dates.py
Line: 234

Comment:
**Underscore prefix still leaks in query echo**

This line serializes parsed `Airline` enum names directly into the JSON `query` field. For digit-prefixed codes like `Airline._3F`, `airline.name` returns `"_3F"` rather than the user-supplied `"3F"`. This is one of the serialization paths the PR description explicitly targets ("digit-prefixed airline codes … still leaking into … CLI output"), but it was missed here.

The `flights` command works correctly because it echoes the raw input strings before parsing. The `dates` command echoes the post-parsed `Airline.name`, so the prefix leaks.

```suggestion
            "airlines": [airline.name.removeprefix("_") for airline in parsed_airlines] if parsed_airlines else None,
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (2): Last reviewed commit: "fix: strip underscore prefix from digit-..." | Re-trigger Greptile

(4/5) You can add custom instructions or style guidelines for the agent here!

Copy link
Copy Markdown
Owner

@punitarani punitarani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm just need to fix merge conflict

i can test locally and merge once fixed

…ization

  paths

  - Use removeprefix("_") instead of lstrip("_") for safer single-prefix
  removal
  - Fix date search serialization in dates.py (was still sending _3F to API)
  - Fix round-trip selected_flight serialization in flights.py: pass enum
    objects to serialize() instead of pre-resolving .name (which bypassed
    the prefix-stripping logic)
  - Add test for serialize_airline with numeric-prefix code
@ktrufanov-skycop
Copy link
Copy Markdown
Contributor Author

Superseded — rebased onto current main in a new PR.

@ktrufanov-skycop
Copy link
Copy Markdown
Contributor Author

CI failure on Python 3.13 is a network timeout in test_complex_round_trip_search (curl timeout hitting Google Flights API) — not related to this change. All other matrix jobs (3.10, 3.11, 3.12) and lint pass cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants