def compute_completion_metadata(shell, matches, inspect_magics=True):
    """Computes completion item metadata.

  Args:
    shell: IPython shell
    matches: List of string completion matches.
    inspect_magics: (optional, default: True) If unset, don't call
        object_inspect on any symbols starting with %.

  Returns:
    Metadata for each of the matches.
  """
    old_str_detail_level = shell.inspector.str_detail_level
    shell.inspector.str_detail_level = 1
    try:
        infos = []
        for match in matches:
            info = {}
            if '#' in match:
                info['type_name'] = match.split('#')[1]
            elif match.startswith('%') and not inspect_magics:
                info['type_name'] = 'Magic function'
            else:
                inspect_results = shell.object_inspect(match)
                info['type_name'] = inspect_results['type_name']
                if inspect_results.get('definition', ''):
                    info['definition'] = inspect_results['definition']
                elif inspect_results.get('init_definition', ''):
                    info['definition'] = inspect_results['init_definition']
            infos.append(info)
        return infos
    finally:
        shell.inspector.str_detail_level = old_str_detail_level
