AndreaCensi / contracts

PyContracts is a Python package that allows to declare constraints on function parameters and return values. Contracts can be specified using Python3 annotations, or inside a docstring. PyContracts supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts and an extension API.
http://andreacensi.github.io/contracts/
Other
398 stars 62 forks source link

Better context in tracebacks when contracts fail? #31

Open ChrisBeaumont opened 9 years ago

ChrisBeaumont commented 9 years ago

The exception messages on contract failures can be very long, which makes it more difficult to find the context of the code where the contract failed. I wonder if the standard contract message should try to re-embed some of the lines surrounding the failure location towards the bottom of the exception message?

AndreaCensi commented 9 years ago

Yes!
I believe there should be a way to make exceptions more friendly. I can see a few different improvements: 1) choose to show shorter messages (like not showing all the recursive "proof" of contract violation) 2) showing more context, like the signature of the function + the function call. 3) censoring out the pycontracts-specific stack frames from the exceptions.