From 3da120fd28aacc4ae44320e45d8d7fbeb9cb2568 Mon Sep 17 00:00:00 2001 From: "Lt. Byte" Date: Thu, 1 Oct 2026 23:55:00 +0200 Subject: [PATCH] style(nav): add type hints --- nav.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nav.py b/nav.py index 8b01a2c..13b74e5 100644 --- a/nav.py +++ b/nav.py @@ -6,6 +6,7 @@ Usage: python3 nav.py DESTINATION """ import sys +from collections.abc import Iterator # Distances between neighboring waypoints, in million km. DISTANCES = { @@ -74,20 +75,20 @@ FUEL_PER_MKM = 0.142 # tonnes of fuel per million km LAUNCH_FUEL = 380.0 # tonnes of fuel to escape Earth's gravity -def distance(a, b): +def distance(a: str, b: str) -> float: """Return the distance between two neighboring waypoints.""" if (a, b) in DISTANCES: return DISTANCES[(a, b)] return DISTANCES[(b, a)] -def legs(route): +def legs(route: list[str]) -> Iterator[tuple[str, str]]: """Yield the legs (pairs of consecutive waypoints) of a route.""" for i in range(1, len(route) - 1): yield route[i], route[i + 1] -def plan(destination): +def plan(destination: str) -> tuple[list[str], float, float, float]: """Return route, distance, travel time (days) and fuel (t).""" route = ROUTES[destination] total = 0.0 @@ -101,12 +102,12 @@ def plan(destination): return route, total, total / CRUISE_SPEED, fuel -def asteroid_route(belt): +def asteroid_route(belt: str) -> list[str]: """Return a route through the asteroid belt (currently: via Ceres).""" return ROUTES["ceres"] + [belt] -def print_plan(destination): +def print_plan(destination: str) -> None: """Print the flight plan for a destination.""" route, total, days, fuel = plan(destination) print("Route: " + " -> ".join(NAMES[w] for w in route)) @@ -115,7 +116,7 @@ def print_plan(destination): print(f"Fuel required: {round(fuel)} t") -def main(): +def main() -> None: if len(sys.argv) != 2: sys.exit(__doc__) if sys.argv[1] == "--list":