Resolve inherited sections

Write codeMedium15 min

Your task

Environments in a deploy file can borrow from one another. The file is sectioned: "[title]" opens a section, "key=value" (first "=", trimmed) assigns inside it, blanks and "#" lines do nothing, repeated keys keep the last value. Inside a section, the special key "inherit" names ONE other section to borrow from. Resolve lookup(section, key) as: the section's own value if assigned; otherwise the borrowed section's own value if that section exists and assigned it; otherwise "". Borrowing is single-hop, never follow the borrowed section's own "inherit". Lookups are never for the key "inherit" itself.

Examples

lookup_with_inherit(lines: list[str], section: str, key: str) -> str
Example 1
Inputlookup_with_inherit(["[base]", "timeout=30", "[prod]", "inherit=base", "host=p.example"], "prod", "timeout")
Output"30"