parameters: (entries)
return_statement: return index
docstring: Returns the distinguishing argument index for a sequence of entries.
  
      Entries are elements of the effective overload set with the same number
      of arguments (formally, same type list length), each a 3-tuple of the form
      (callable, type list, optionality list).
  
      Spec: http://heycam.github.io/webidl/#dfn-distinguishing-argument-index
  
      If there is more than one entry in an effective overload set that has a
      given type list length, then for those entries there must be an index i
      such that for each pair of entries the types at index i are
      distinguishable.
      The lowest such index is termed the distinguishing argument index for the
      entries of the effective overload set with the given type list length.
docstring_summary: Returns the distinguishing argument index for a sequence of entries.
docstring_tokens: Returns the distinguishing argument index for a sequence of entries .
function: def distinguishing_argument_index(entries):
      """Returns the distinguishing argument index for a sequence of entries.
  
      Entries are elements of the effective overload set with the same number
      of arguments (formally, same type list length), each a 3-tuple of the form
      (callable, type list, optionality list).
  
      Spec: http://heycam.github.io/webidl/#dfn-distinguishing-argument-index
  
      If there is more than one entry in an effective overload set that has a
      given type list length, then for those entries there must be an index i
      such that for each pair of entries the types at index i are
      distinguishable.
      The lowest such index is termed the distinguishing argument index for the
      entries of the effective overload set with the given type list length.
      """
      # Only applicable “If there is more than one entry”
      assert len(entries) > 1
      type_lists = [tuple(idl_type.name for idl_type in entry[1])
                    for entry in entries]
      type_list_length = len(type_lists[0])
      # Only applicable for entries that “[have] a given type list length”
      assert all(len(type_list) == type_list_length for type_list in type_lists)
      name = entries[0][0].get('name', 'Constructor')  # for error reporting
  
      # The spec defines the distinguishing argument index by conditions it must
      # satisfy, but does not give an algorithm.
      #
      # We compute the distinguishing argument index by first computing the
      # minimum index where not all types are the same, and then checking that
      # all types in this position are distinguishable (and the optionality lists
      # up to this point are identical), since "minimum index where not all types
      # are the same" is a *necessary* condition, and more direct to check than
      # distinguishability.
      types_by_index = (set(types) for types in zip(*type_lists))
      try:
          # “In addition, for each index j, where j is less than the
          #  distinguishing argument index for a given type list length, the types
          #  at index j in all of the entries’ type lists must be the same”
          index = next(i for i, types in enumerate(types_by_index)
                       if len(types) > 1)
      except StopIteration:
          raise ValueError('No distinguishing index found for %s, length %s:\n'
                           'All entries have the same type list:\n'
                           '%s' % (name, type_list_length, type_lists[0]))
      # Check optionality
      # “and the booleans in the corresponding list indicating argument
      #  optionality must be the same.”
      # FIXME: spec typo: optionality value is no longer a boolean
      # https://www.w3.org/Bugs/Public/show_bug.cgi?id=25628
      initial_optionality_lists = set(entry[2][:index] for entry in entries)
      if len(initial_optionality_lists) > 1:
          raise ValueError(
              'Invalid optionality lists for %s, length %s:\n'
              'Optionality lists differ below distinguishing argument index %s:\n'
              '%s'
              % (name, type_list_length, index, set(initial_optionality_lists)))
  
      # Check distinguishability
      # http://heycam.github.io/webidl/#dfn-distinguishable
      # Use names to check for distinct types, since objects are distinct
      # FIXME: check distinguishability more precisely, for validation
      distinguishing_argument_type_names = [type_list[index]
                                            for type_list in type_lists]
      if (len(set(distinguishing_argument_type_names)) !=
          len(distinguishing_argument_type_names)):
          raise ValueError('Types in distinguishing argument are not distinct:\n'
                           '%s' % distinguishing_argument_type_names)
  
      return index
function_tokens: def distinguishing_argument_index ( entries ) : # Only applicable “If there is more than one entry” assert len ( entries ) > 1 type_lists = [ tuple ( idl_type . name for idl_type in entry [ 1 ] ) for entry in entries ] type_list_length = len ( type_lists [ 0 ] ) # Only applicable for entries that “[have] a given type list length” assert all ( len ( type_list ) == type_list_length for type_list in type_lists ) name = entries [ 0 ] [ 0 ] . get ( 'name' , 'Constructor' ) # for error reporting # The spec defines the distinguishing argument index by conditions it must # satisfy, but does not give an algorithm. # # We compute the distinguishing argument index by first computing the # minimum index where not all types are the same, and then checking that # all types in this position are distinguishable (and the optionality lists # up to this point are identical), since "minimum index where not all types # are the same" is a *necessary* condition, and more direct to check than # distinguishability. types_by_index = ( set ( types ) for types in zip ( * type_lists ) ) try : # “In addition, for each index j, where j is less than the #  distinguishing argument index for a given type list length, the types #  at index j in all of the entries’ type lists must be the same” index = next ( i for i , types in enumerate ( types_by_index ) if len ( types ) > 1 ) except StopIteration : raise ValueError ( 'No distinguishing index found for %s, length %s:\n' 'All entries have the same type list:\n' '%s' % ( name , type_list_length , type_lists [ 0 ] ) ) # Check optionality # “and the booleans in the corresponding list indicating argument #  optionality must be the same.” # FIXME: spec typo: optionality value is no longer a boolean # https://www.w3.org/Bugs/Public/show_bug.cgi?id=25628 initial_optionality_lists = set ( entry [ 2 ] [ : index ] for entry in entries ) if len ( initial_optionality_lists ) > 1 : raise ValueError ( 'Invalid optionality lists for %s, length %s:\n' 'Optionality lists differ below distinguishing argument index %s:\n' '%s' % ( name , type_list_length , index , set ( initial_optionality_lists ) ) ) # Check distinguishability # http://heycam.github.io/webidl/#dfn-distinguishable # Use names to check for distinct types, since objects are distinct # FIXME: check distinguishability more precisely, for validation distinguishing_argument_type_names = [ type_list [ index ] for type_list in type_lists ] if ( len ( set ( distinguishing_argument_type_names ) ) != len ( distinguishing_argument_type_names ) ) : raise ValueError ( 'Types in distinguishing argument are not distinct:\n' '%s' % distinguishing_argument_type_names ) return index