Skip to content

fix: raise descriptive error for empty/comments-only config files (#139)#142

Open
AmSach wants to merge 1 commit into
toolsforexperiments:masterfrom
AmSach:fix/issue-139-empty-config-typerror
Open

fix: raise descriptive error for empty/comments-only config files (#139)#142
AmSach wants to merge 1 commit into
toolsforexperiments:masterfrom
AmSach:fix/issue-139-empty-config-typerror

Conversation

@AmSach

@AmSach AmSach commented Jun 14, 2026

Copy link
Copy Markdown

Fixes #139

What was broken

Starting the server with an empty (or comments-only) config file crashed
with an opaque Python error:

File "src/instrumentserver/config.py", line 50, in loadConfig
    if "instruments" not in rawConfig:
TypeError: argument of type 'NoneType' is not iterable

Root cause

ruamel.yaml's yaml.load() returns None when the file is empty
(0 bytes) or contains only YAML comments (no key/value pairs). The
loadConfig function in src/instrumentserver/config.py then ran
"instruments" not in rawConfig immediately on the result, and the
membership test on None raised TypeError.

The next line of the function already raises a clear AttributeError
("All configurations must be inside the 'instruments' field...") when
the key is missing — so a user who simply forgets to add the section
sees a helpful message, but a user with a totally empty or
comments-only file sees a raw Python TypeError instead. Both are
missing-key cases at heart, and they should be reported consistently.

Fix

Added a rawConfig is None guard right after yaml.load(). When
triggered, it raises an AttributeError whose message mirrors the
existing missing-key error and tells the user what to do next:

AttributeError: The config file '/path/to/serverConfig.yml' is empty.
It needs at least an 'instruments:' section.

Tests

Three new regression tests in test/pytest/test_config.py:

  • test_empty_config_raises — empty file raises AttributeError
    whose message contains "empty".
  • test_comments_only_config_raises — comments-only file raises the
    same AttributeError (yaml also returns None for these).
  • test_empty_config_error_mentions_instruments — the error message
    guides the user toward the required 'instruments:' key.

All 21 tests in test_config.py pass.

Files changed

  • src/instrumentserver/config.py — +11 lines, the None guard.
  • test/pytest/test_config.py — +33 lines, three regression tests.

ruamel.yaml's yaml.load() returns None for an empty or comments-only
file. The very next line ("instruments" not in rawConfig) then
evaluated "instruments" not in None and crashed with the opaque
TypeError: argument of type 'NoneType' is not iterable.

Guard for the None case right after the load() call and raise an
AttributeError with a message in the same spirit as the existing
missing-'instruments'-key error, so the user is told the file is
empty and what section it needs.

Fixes toolsforexperiments#139.
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.

loadConfig raises confusing TypeError on an empty config file

1 participant