Skip to content

Validate input file and complain about any unused data items#30

Open
wscott wants to merge 2 commits into
masterfrom
input-validate
Open

Validate input file and complain about any unused data items#30
wscott wants to merge 2 commits into
masterfrom
input-validate

Conversation

@wscott

@wscott wscott commented Jan 27, 2024

Copy link
Copy Markdown
Owner

No description provided.

@wscott

wscott commented Jan 27, 2024

Copy link
Copy Markdown
Owner Author

If I had a regression framework setup, I would add some test cases to demonstrate that extra data generates warning messages.

@wscott

wscott commented Jan 27, 2024

Copy link
Copy Markdown
Owner Author

This code could be simplified with the right helper function, but I ran out of time to mess with this at the moment.

@cmovic

cmovic commented Jan 29, 2024

Copy link
Copy Markdown
Contributor

A simpler or at least less intrusive way to do this is to define a set of known keys and use set difference to see if there are any unknown keys prior to parsing the data. Here's a hacky example:

    def load_file(self, file):
        with open(file) as conffile:
            d = tomllib.loads(conffile.read())
            
        known_keys = {'returns', 'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'}
        # known_keys = {'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'}   # BAD missing key
        # known_keys = {'moose', 'returns', 'inflation', 'startage', 'endage', 'taxes', 'prep', 'income', 'expense', 'aftertax', 'IRA', 'roth'} # OK extra key
        extra_keys = d.keys() - known_keys
        if extra_keys:
            for k in extra_keys:
                print(f'{k} = {d[k]}')
            raise Exception(f'Unknown keys found in config file: {file}')

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.

2 participants