Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, *args):
- self.args = args
- def __repr__(self):
- return str(self.args)
- class List(Node):
- pass
- class Union(Node):
- pass
- class Dict(Node):
- pass
- def iter_concrete_types(obj):
- if isinstance(obj, Union):
- for arg in obj.args:
- yield from iter_concrete_types(arg)
- elif isinstance(obj, List):
- for arg in obj.args:
- for t in iter_concrete_types(arg):
- yield f"List[{t}]"
- elif isinstance(obj, Dict):
- k,v = obj.args
- for tk in iter_concrete_types(k):
- for tv in iter_concrete_types(v):
- yield f"Dict[{tk}, {tv}]"
- elif isinstance(obj, type):
- yield obj.__name__
- else:
- raise Exception(f"Don't know how to concretize object of type {type(obj)}")
- x = Union(List(Union(Dict(str, Union(int, List(int))), Dict(str, Union(Dict(str, Union(int, List(int))), List(int))))), Union(Dict(str, Union(int, List(int))), Dict(str, Union(Dict(str, Union(int, List(int))), List(int)))))
- for t in iter_concrete_types(x):
- print(t)
- """result:
- List[Dict[str, int]]
- List[Dict[str, List[int]]]
- List[Dict[str, Dict[str, int]]]
- List[Dict[str, Dict[str, List[int]]]]
- List[Dict[str, List[int]]]
- Dict[str, int]
- Dict[str, List[int]]
- Dict[str, Dict[str, int]]
- Dict[str, Dict[str, List[int]]]
- Dict[str, List[int]]
- """
Add Comment
Please, Sign In to add comment