Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Errors for Nested Traits #402

Merged
merged 2 commits into from
May 23, 2017
Merged

Conversation

rmorshea
Copy link
Contributor

@rmorshea rmorshea commented May 8, 2017

Summary

Right now nested traits have pretty horrid error messages.

The following code produces three different tracebacks with three bad error messages:

class MyClass(HasTraits):
    
    l = List(List(Int()))

mc = MyClass()
mc.l = [["value"]]

Error messages (tracebacks excluded):

The 'None' trait of a MyClass instance must be an int, but a value of 'str' <class 'str'> was specified.``
Element of the 'None' trait of a MyClass instance must be an int, but a value of 'str' <class 'str'> was specified.
Element of the 'l' trait of a MyClass instance must be a list, but a value of ['str'] <class 'list'> was specified.

The First two messages, while accurate, don't have a trait name associated with them which makes them confusing to read. The last one has a trait name but is inaccurate since it complains of a list being provided to a List trait!

Solution

A generic TraitType.error(obj, value, error=None) method that can be used to easily handle nested errors. To use it, one only needs to catch the nested error in a parent trait and pass it to error.

With the proposed changes, the code above would create one concise traceback and message:

TraitError: The 'l' trait of a MyClass instance contains an Int of a List of a List which expected an int, not the str 'value'

Notes

There is an added utility file descriptions.py which has a new describe function in addition to others that were previously present in traitlets.py like add_article or repr_type. The describe function provides a common way to represent a value as a readable string (see docstring for use details).

@rmorshea rmorshea force-pushed the nested_errors branch 5 times, most recently from f66e46c to c5e5e78 Compare May 8, 2017 20:31
@rmorshea
Copy link
Contributor Author

rmorshea commented May 19, 2017

@minrk, this should probably get the 5.0 milestone.

@minrk minrk added this to the 5.0 milestone May 20, 2017
@rmorshea
Copy link
Contributor Author

@minrk, this now passes after #406.

@minrk
Copy link
Member

minrk commented May 23, 2017

Thanks, @rmorshea!

@minrk minrk merged commit 7f3814c into ipython:master May 23, 2017
@Carreau Carreau added 5.0-re-review Need to re-review for potential API impact changes. 5.0-major Major change in 5.0 need proper documentation and removed 5.0-re-review Need to re-review for potential API impact changes. labels Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0-major Major change in 5.0 need proper documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants