If I want to match against a list containing 2 elements (1 str, 1 bool) I can do the following:
match some_lst:
case [str(), bool()]: # -> Valid
do_something()
How can I apply the same logic to dictionaries without using guards? For example, this doesn't work:
match some_dict:
case {str(): bool()}: # -> This is invalid
do_something()
Working example with guard:
match some_dict:
case dict() if all(isinstance(k, str) and isinstance(v, bool) for k, v in some_dict.items()):
do_something() # -> This works
In python, you can't easily enforce keys type and values type for your dict.
This means python has no means to verify all the keys and all the values from your statement:
You could consider doing some kind of switch case with TypedDict, however, you can't use anystring keys with this type, you are force to define each of your keys which can often be unwanted.
Nevertheless here are 2 ways you could attack your problem:
PYTHON 3.8 with jsonschema
Feel free to improve indentation level and complexity from this solution if you manage to do so.
PYTHON 3.10 iterate dict keys, values and using type to string
This ain't perfect, I am just moving your loop but at least you don't run the loop for each case: