def _is_named_tuple(x):
  """Check if we're dealing with a NamedTuple."""
  t = type(x)
  b = t.__bases__
  if len(b) != 1 or b[0] != tuple:
    return False
  f = getattr(t, "_fields", None)
  if not isinstance(f, tuple):
    return False
  return all(isinstance(n, str) for n in f)
Check if we're dealing with a NamedTuple.
language/canine/tensor_contracts.py