fix: raise descriptive error for empty/comments-only config files (#139)#142
Open
AmSach wants to merge 1 commit into
Open
fix: raise descriptive error for empty/comments-only config files (#139)#142AmSach wants to merge 1 commit into
AmSach wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #139
What was broken
Starting the server with an empty (or comments-only) config file crashed
with an opaque Python error:
Root cause
ruamel.yaml'syaml.load()returnsNonewhen the file is empty(0 bytes) or contains only YAML comments (no key/value pairs). The
loadConfigfunction insrc/instrumentserver/config.pythen ran"instruments" not in rawConfigimmediately on the result, and themembership test on
NoneraisedTypeError.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
TypeErrorinstead. Both aremissing-key cases at heart, and they should be reported consistently.
Fix
Added a
rawConfig is Noneguard right afteryaml.load(). Whentriggered, it raises an
AttributeErrorwhose message mirrors theexisting missing-key error and tells the user what to do next:
Tests
Three new regression tests in
test/pytest/test_config.py:test_empty_config_raises— empty file raisesAttributeErrorwhose message contains "empty".
test_comments_only_config_raises— comments-only file raises thesame
AttributeError(yaml also returnsNonefor these).test_empty_config_error_mentions_instruments— the error messageguides the user toward the required
'instruments:'key.All 21 tests in
test_config.pypass.Files changed
src/instrumentserver/config.py— +11 lines, theNoneguard.test/pytest/test_config.py— +33 lines, three regression tests.