-
Notifications
You must be signed in to change notification settings - Fork 122
Adds tests for the current state of the project when using TOML #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds tests to improve the project's TOML configuration parsing while increasing overall test coverage. Key changes include:
- New test classes for TOML and composite configuration parsing.
- Additional tests to validate behavior with various TOML file conditions.
- An update to setup.py to include the toml package in test dependencies.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_configargparse.py | Adds new tests for TOML parsing and composite configuration error handling. |
| setup.py | Updates test requirements to include the toml package. |
Comments suppressed due to low confidence (1)
tests/test_configargparse.py:1895
- [nitpick] Consider adding an inline comment to explain why appending extra keys in the TOML file is expected to trigger a SystemExit, which will help clarify the test intent.
def test_toml_extra(self):
| def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"): | ||
| with open("config.yaml", "w") as f: | ||
| f.write(content) | ||
|
|
||
| def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"): | ||
| with open("config.ini", "w") as f: | ||
| f.write(content) | ||
|
|
||
| def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"): | ||
| with open("config.toml", "w") as f: | ||
| f.write(content) |
Copilot
AI
Jun 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider refactoring the repeated file writing helper methods (for YAML, INI, and TOML) into a shared utility to reduce duplication across tests.
| def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"): | |
| with open("config.yaml", "w") as f: | |
| f.write(content) | |
| def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"): | |
| with open("config.ini", "w") as f: | |
| f.write(content) | |
| def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"): | |
| with open("config.toml", "w") as f: | |
| f.write(content) | |
| def write_config_file(self, filename, content): | |
| """Writes the given content to the specified file.""" | |
| with open(filename, "w") as f: | |
| f.write(content) | |
| def write_yaml_file(self, content="[section]\nkey1: yaml1\nkey2: yaml2"): | |
| self.write_config_file("config.yaml", content) | |
| def write_ini_file(self, content="[section]\nkey1=ini1\nkey2=ini2"): | |
| self.write_config_file("config.ini", content) | |
| def write_toml_file(self, content="[section]\nkey1 = 'toml1'\nkey2 = 'toml2'"): | |
| self.write_config_file("config.toml", content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you not heard of StringIO, copilot?
bw2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! @tristanlatr @tbooth any objections to merging this?
tests/test_configargparse.py
Outdated
| default_config_files=['config.yaml', 'config.toml', 'config.ini'], | ||
| config_file_parser_class=configargparse.CompositeConfigParser( | ||
| [ | ||
| configargparse.TomlConfigParser(['section']), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this line is removed the tests all still pass, so either remove the line or make it necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort of. Issue is that IniConfigParser picks up the slack, since the two are structured very similarly. If you comment the other two out, and run the test for just pytest -s tests/test_configargparse.py::TestCompositeConfigParser::test_toml it passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, so just include something in the TOML snippet that would not be properly recognised by the INI parser, and that will make the test much more useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For that i would need to touch the code, there is a bug that will cause a crash raising ConfigFileParserException.
This is the best i can do without touching anything else, and focusing the changes VERY narrowly.
I plan on fixing this issue after the other toml PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine IMO for these new tests you are adding to fail with the current code. Adding correct but failing tests is the motivator for accepting the patches to fix them!
I'd suggest adding a line like foo=0xDEADBEEF to the sample TOML snippet in your test. The INI parser will parse this incorrectly as "0xDEADBEEF". The old toml module will (it appears) raise an exception. The newer tomli module you want to introduce will correctly parse it as 3735928559, which should (but will currently not) be converted to a string :"3735928559"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I agree with what you say, but if its all the same, i'd like to do this in another PR. Can you approve it... if you deem it... worthy, so i can continue... pretty please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While youre at it, there are another 2 prs open as of 20mins ago, SUPER tiny. low risk. -- if you can take a quick look, id appreciate it immensely.
tests/test_configargparse.py
Outdated
| """) | ||
| parser = configargparse.TomlConfigParser(['tool.section']) | ||
| with open('config.toml', 'r') as f: | ||
| self.assertEqual(parser.parse(f), {'key1': "toml1", 'key2': [1, 2, 3]}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted by @tristanlatr this behaviour is incorrect so please change it to 'key2': ['1', '2', '3'] and then this test should (correctly) fail. If we want to force a module release before fixing it then it can be marked as an expected failure.
I fixed this in my own fork by having a unified config data cleanup function, rather then relying on each separate parser class to do its own cleanup. I would advocate adopting this change to resolve the failure, rather then writing a new quick fix to the TOML parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
tests/test_configargparse.py
Outdated
|
|
||
|
|
||
| class TestTomlConfigParser(unittest.TestCase): | ||
| def setUp(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This temp file creation is a lot of work compared to using io.StringIO. Also, changing directory within a unit test is best avoided unless strictly necessary. Not a major problem - I can sort this out in a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converted most..
|
I added some comments but the one that really needs attention is the one on line 1834. The test purports to be testing composite parsing with the TOML parser but removing that parser from the list does not cause the tests to fail. |
|
@tbooth anything missing from your perspective? |
|
@tbooth thanks for the merge. Next time please squash my commits, there is no need for so much noise in the commit history in the master branch:
The major issue of not squashing is that now all the PRs have conflicts now... |

Adds tests for the current state of the application, no logic changes
Increases the coverage from 85% (master) to 91%