Is it possible in mypy
or Python3.6+
or in any of their extensions to specify that my function can take any dict as a parameter as long as it has specific keys in it? If there is an hypothetical type Record
For e.g.
def full_name(my_dict: Record[first_name: str, last_name: str]) -> str:
return my_dict['last_name'] + ',' + my_dict['first_name']
valid_dict = {'first_name': 'John', 'last_name': 'Doe'}
big_and_valid_dict = {'first_name': 'Matt', 'last_name': 'Legi', 'age': 32}
invalid_dict1 = {'first_name': 'BadName'} # No 'last_name' key
invalid_dict2 = {'no_name': 'NoName'} # none of the specified keys
So, static analysis should report the following as
full_name(valid_dict) # => works
full_name(big_and_valid_dict) # => works
full_name(invalid_dict1) # => error
full_name(invalid_dict2) # => error