in tfx/dsl/component/experimental/json_compat.py [0:0]
def check(typehint: Any, not_primitive: bool = True) -> bool:
origin = getattr(typehint, '__origin__', typehint)
args = getattr(typehint, '__args__', None)
if origin is dict or origin is list or origin is Union:
# Starting from Python 3.9 Dict won't have default args (~KT, ~VT)
# and List won't have default args (~T).
if not args:
return False
elif origin is dict and args[0] is not str:
return False
elif origin is dict and args[0] is str:
return check(typehint=args[1], not_primitive=False)
# Handle top level optional.
elif origin is Union and not_primitive:
return all([
arg is type(None) or
check(typehint=arg, not_primitive=True) for arg in args
])
else:
return all([check(typehint=arg, not_primitive=False) for arg in args])
else:
return not not_primitive and origin in _JSON_COMPATIBLE_PRIMITIVES