parameters: (message_descriptor, cls)
docstring: Helper for _AddMessageMethods().
docstring_summary: Helper for _AddMessageMethods().
docstring_tokens: Helper for _AddMessageMethods () .
function: def _AddHasFieldMethod(message_descriptor, cls):
    """Helper for _AddMessageMethods()."""
  
    singular_fields = {}
    for field in message_descriptor.fields:
      if field.label != _FieldDescriptor.LABEL_REPEATED:
        singular_fields[field.name] = field
  
    def HasField(self, field_name):
      try:
        field = singular_fields[field_name]
      except KeyError:
        raise ValueError(
            'Protocol message has no singular "%s" field.' % field_name)
  
      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
        value = self._fields.get(field)
        return value is not None and value._is_present_in_parent
      else:
        return field in self._fields
    cls.HasField = HasField
function_tokens: def _AddHasFieldMethod ( message_descriptor , cls ) : singular_fields = { } for field in message_descriptor . fields : if field . label != _FieldDescriptor . LABEL_REPEATED : singular_fields [ field . name ] = field def HasField ( self , field_name ) : try : field = singular_fields [ field_name ] except KeyError : raise ValueError ( 'Protocol message has no singular "%s" field.' % field_name ) if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : value = self . _fields . get ( field ) return value is not None and value . _is_present_in_parent else : return field in self . _fields cls . HasField = HasField