Skip to content

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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

2 participants