[Python-ideas] introspectable assertions
Steven D'Aprano
steve at pearwood.info
Thu Feb 19 11:56:05 CET 2015
On Thu, Feb 19, 2015 at 08:16:33AM +0100, Ronny Pfannschmidt wrote:
>
> Hi,
>
> the idea is that if
>
> -> from __future__ import assertion_introspection
>
> then
>
> -> assert foo.bar(baz) == abc.def(), msg
> DetailedAssertionError: {msg}
> foo -> <foo...>
> baz -> <bar ...>
> foo.bar() -> <...>
> abc -> ...
> abc.def() -> ...
> foo.bar(baz) == abc.def() -> ...
>
> it would help introspection and testing utilities
I don't think we need a __future__ import, or to special case assert.
Python already has hooks for customizing tracebacks, see the cgitb
module. With it:
py> assert 23 == 24
AssertionError
Python 3.3.0rc3: /usr/local/bin/python3.3
Thu Feb 19 21:51:04 2015
A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred.
/home/steve/<stdin> in <module>()
AssertionError:
__cause__ = None
__class__ = <class 'AssertionError'>
__context__ = None
__delattr__ = <method-wrapper '__delattr__' of AssertionError object>
__dict__ = {}
__dir__ = <built-in method __dir__ of AssertionError object>
__doc__ = 'Assertion failed.'
__eq__ = <method-wrapper '__eq__' of AssertionError object>
__format__ = <built-in method __format__ of AssertionError object>
__ge__ = <method-wrapper '__ge__' of AssertionError object>
__getattribute__ = <method-wrapper '__getattribute__' of AssertionError object>
__gt__ = <method-wrapper '__gt__' of AssertionError object>
__hash__ = <method-wrapper '__hash__' of AssertionError object>
__init__ = <method-wrapper '__init__' of AssertionError object>
__le__ = <method-wrapper '__le__' of AssertionError object>
__lt__ = <method-wrapper '__lt__' of AssertionError object>
__ne__ = <method-wrapper '__ne__' of AssertionError object>
__new__ = <built-in method __new__ of type object>
__reduce__ = <built-in method __reduce__ of AssertionError object>
__reduce_ex__ = <built-in method __reduce_ex__ of AssertionError object>
__repr__ = <method-wrapper '__repr__' of AssertionError object>
__setattr__ = <method-wrapper '__setattr__' of AssertionError object>
__setstate__ = <built-in method __setstate__ of AssertionError object>
__sizeof__ = <built-in method __sizeof__ of AssertionError object>
__str__ = <method-wrapper '__str__' of AssertionError object>
__subclasshook__ = <built-in method __subclasshook__ of type object>
__suppress_context__ = False
__traceback__ = <traceback object>
args = ()
with_traceback = <built-in method with_traceback of AssertionError object>
The above is a description of an error in a Python program. Here is
the original traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
I don't know that I would actually use the cgitb module in production,
it appears to display a lot of noise, but I think it is a good place to
start.
--
Steve
More information about the Python-ideas
mailing list