Skip to content

fix(us): use caller-supplied pElev instead of crashing on it - #391

Merged
johannesparty merged 1 commit into
mainfrom
fix/rank-soils-caller-elevation
Sep 25, 2026
Merged

johannesparty merged 1 commit into
mainfrom
fix/rank-soils-caller-elevation

Conversation

@johannesparty

Copy link
Copy Markdown
Contributor

What

rank_soils crashed with UnboundLocalError when the caller supplies pElev (a point elevation) instead of letting the function look one up.

# before — pElev_dict only assigned when pElev is None, but parsed unconditionally:
if pElev is None:
    pElev_dict = get_elev_data(lon, lat)
try:
    pElev = float(pElev_dict["value"])      # UnboundLocalError when caller passed pElev
except (KeyError, TypeError, ValueError):    # ...and this never catches UnboundLocalError
    pElev = None

Fix: move the lookup + parse inside the if pElev is None: guard, so a caller-provided pElev is used as-is.

Why now

The terraso-backend soil-ID elevation feature (feeding the client's elevation into the US Site score) is the first caller to actually pass pElev. Until now pElev was always None, so the buggy branch was never exercised.

Test

Added test_rank_soils_accepts_caller_supplied_elevation — calls rank_soils with a numeric pElev (which skips get_elev_data, so it's network-free and deterministic) and asserts it returns without raising. The full non-snapshot suite (157 tests) passes.

🤖 Generated with Claude Code

rank_soils assigned pElev_dict only in the `pElev is None` branch but
dereferenced it unconditionally, so a caller-supplied pElev raised
UnboundLocalError (not caught by the surrounding except). Move the
lookup+parse inside the `if pElev is None:` guard so a provided
elevation is used as-is.

Add a regression test that calls rank_soils with a numeric pElev.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@johannesparty
johannesparty merged commit 7a0b88e into main Sep 25, 2026
4 checks passed
@johannesparty
johannesparty deleted the fix/rank-soils-caller-elevation branch September 25, 2026 17:07
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