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

Improve RepresenterError creation #55

Closed
wants to merge 1 commit into from
Closed

Improve RepresenterError creation #55

wants to merge 1 commit into from

Conversation

@bondarevts
Copy link
Contributor

@bondarevts bondarevts commented Mar 1, 2017

Passing data as an parameter for RepresenterError allows user to make some research about nature of a failed object.

Before:

>>> import yaml
>>> import numpy as np
>>> lst = list(map(np.int16, range(5, 10)))
>>> lst
[5, 6, 7, 8, 9]
>>> try:
...     yaml.dump(lst, Dumper=yaml.SafeDumper)
... except yaml.representer.RepresenterError as e:
...     print(e)
cannot represent an object: 5

Nothing we can do to understand why 5 can't be represented.

After:

>>> try:
...     yaml.dump(lst, Dumper=yaml.SafeDumper)
... except yaml.representer.RepresenterError as e:
...     print(e)
...     if len(e.args) > 1:
...         bad_object = e.args[1]
...         print("Can't represent an object %r of type %s" % (bad_object, type(bad_object)))
...
('cannot represent an object', 5)
Can't represent an object 5 of type <class 'numpy.int16'>

It's even better to have an Exception class with explicit argument for bad object, but I decided to start with smaller changes.

@sigmavirus24
Copy link
Contributor

@sigmavirus24 sigmavirus24 commented Aug 8, 2017

This is excellent @bondarevts! Thanks!

Loading

@sigmavirus24
Copy link
Contributor

@sigmavirus24 sigmavirus24 commented Aug 8, 2017

This was rebased and merged in ef744d8. Thanks again!

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants