Open wscott opened 5 months ago
This code could be simplified with the right helper function, but I ran out of time to mess with this at the moment.
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}')
If I had a regression framework setup, I would add some test cases to demonstrate that extra data generates warning messages.