Skip to content

Conversation

@wscott
Copy link
Owner

@wscott wscott commented Jan 27, 2024

No description provided.

@wscott
Copy link
Owner Author

wscott commented Jan 27, 2024

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

@wscott
Copy link
Owner Author

wscott commented Jan 27, 2024

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

@cmovic
Copy link
Contributor

cmovic commented Jan 29, 2024

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.

3 participants