# Copyright (C) 2016 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
from .exceptions import ValidationError

def validate_option(model, attribute, option, desired_role):
  if option and option.role != desired_role:
    message = "Invalid value for attribute {}.{}. "\
    "Expected option with role {}, received role {}.".format(model, attribute, desired_role, option.role)
    raise ValidationError(message)
  return option


class PolymorphicRelationship(object):
  """This is shortcut to generate relationships from type and id fields."""
  def __init__(self, id_attr, type_attr, backref_format):
    self._id_attr = id_attr
    self._type_attr = type_attr
    self._backref_format = backref_format

  def _make_backref_attr(self, obj):
    """Construct the name of backref attr from format and type field value."""
    return self._backref_format.format(getattr(obj, self._type_attr))

  def __get__(self, obj, cls):
    """Get the value of the backref attr."""
    if obj is None:
      return self
    else:
      return getattr(obj, self._make_backref_attr(obj))

  def __set__(self, obj, value):
    """Prepare and set the value of the backref attr."""
    if value is None:
      setattr(obj, self._id_attr, None)
      setattr(obj, self._type_attr, None)
    else:
      setattr(obj, self._id_attr, value.id)
      setattr(obj, self._type_attr, value.__class__.__name__)
      setattr(obj, self._make_backref_attr(obj), value)

@@ -146,16 +146,17 @@
 nError%0A%0A
+%0A
 def vali
@@ -267,16 +267,17 @@
 ssage = 
+(
 %22Invalid
@@ -309,10 +309,20 @@
 %7D. %22
-%5C
 %0A
+           
     
@@ -370,16 +370,32 @@
 ole %7B%7D.%22
+%0A               
 .format(
@@ -438,16 +438,17 @@
 on.role)
+)
 %0A    rai

Fix flake8 errors