diff options
| author | Jason Madden <jamadden@gmail.com> | 2020-02-06 10:48:04 -0600 |
|---|---|---|
| committer | Jason Madden <jamadden@gmail.com> | 2020-02-07 10:59:11 -0600 |
| commit | a825e5f29e98c8c84076600323ffe791531ac765 (patch) | |
| tree | fbc45814c37382cfa825e4a5f9161cab571f77e2 /src/zope/interface/exceptions.py | |
| parent | cc537c613ba6dd1a076202e2144fdacfccc520d2 (diff) | |
| download | zope-interface-a825e5f29e98c8c84076600323ffe791531ac765.tar.gz | |
Make verification errors more readable and useful.
Eliminate the trailing newlines and blank spaces (the code called them
"a stupid artifact").
Include the name of the defining interface (so the user can easily look up
any requirements on the attribute) and, for methods, the expected
signature (no more guessing about how many arguments are required!).
This is implemented by giving Attribute and Method useful reprs and strs.
Previously, they just had the defaults.
Fixes #170
Diffstat (limited to 'src/zope/interface/exceptions.py')
| -rw-r--r-- | src/zope/interface/exceptions.py | 86 |
1 files changed, 65 insertions, 21 deletions
diff --git a/src/zope/interface/exceptions.py b/src/zope/interface/exceptions.py index 9be1b8c..8d21f33 100644 --- a/src/zope/interface/exceptions.py +++ b/src/zope/interface/exceptions.py @@ -29,42 +29,86 @@ class Invalid(Exception): """A specification is violated """ -class DoesNotImplement(Invalid): - """ This object does not implement """ - def __init__(self, interface): +_NotGiven = object() + +class _TargetMixin(object): + target = _NotGiven + + @property + def _prefix(self): + if self.target is _NotGiven: + return "An object" + return "The object %r" % (self.target,) + +class DoesNotImplement(Invalid, _TargetMixin): + """ + The *target* (optional) does not implement the *interface*. + + .. versionchanged:: 5.0.0 + Add the *target* argument and attribute, and change the resulting + string value of this object accordingly. + """ + + def __init__(self, interface, target=_NotGiven): + Invalid.__init__(self) self.interface = interface + self.target = target def __str__(self): - return """An object does not implement interface %(interface)s + return "%s does not implement the interface %s." % ( + self._prefix, + self.interface + ) - """ % self.__dict__ +class BrokenImplementation(Invalid, _TargetMixin): + """ + The *target* (optional) is missing the attribute *name*. + + .. versionchanged:: 5.0.0 + Add the *target* argument and attribute, and change the resulting + string value of this object accordingly. -class BrokenImplementation(Invalid): - """An attribute is not completely implemented. + The *name* can either be a simple string or a ``Attribute`` object. """ - def __init__(self, interface, name): - self.interface=interface - self.name=name + def __init__(self, interface, name, target=_NotGiven): + Invalid.__init__(self) + self.interface = interface + self.name = name + self.target = target def __str__(self): - return """An object has failed to implement interface %(interface)s + return "%s has failed to implement interface %s: The %s attribute was not provided." % ( + self._prefix, + self.interface, + repr(self.name) if isinstance(self.name, str) else self.name + ) - The %(name)s attribute was not provided. - """ % self.__dict__ +class BrokenMethodImplementation(Invalid, _TargetMixin): + """ + The *target* (optional) has a *method* that violates + its contract in a way described by *mess*. + + .. versionchanged:: 5.0.0 + Add the *target* argument and attribute, and change the resulting + string value of this object accordingly. -class BrokenMethodImplementation(Invalid): - """An method is not completely implemented. + The *method* can either be a simple string or a ``Method`` object. """ - def __init__(self, method, mess): - self.method=method - self.mess=mess + def __init__(self, method, mess, target=_NotGiven): + Invalid.__init__(self) + self.method = method + self.mess = mess + self.target = target def __str__(self): - return """The implementation of %(method)s violates its contract - because %(mess)s. - """ % self.__dict__ + return "%s violates its contract in %s: %s." % ( + self._prefix, + repr(self.method) if isinstance(self.method, str) else self.method, + self.mess + ) + class InvalidInterface(Exception): """The interface has invalid contents |
