I would want to create custom type checker to my type where I could define my own type checker function to check complex rules:
For example, I have my custom type interited from dict. This custom dict can contain dicts, ints, strings or lists. Nested dicts can contain lists, strings ints or dicts etc. BUT list can only contain dicts.
To achieve the functionality described above I would want to build function which iterate trough my customed dict and checks that there is only dicts direct descendant of lists. For example:
class MyType(dict):
def __override_my_type_type_check(self):
for item in nested_dicts_and_lists:
if isinstance(item, list):
for sub_item in item:
if not isinstance(sub_item, dict):
# Type check failed
return False
# Type check passed
return True
foo = MyType({"bar": ["foo", {"bar": "foo"}]})
isinstance(foo, MyType) # should return False because list contains string instead of only dicts