Skip to content

Improve RepresenterError creation#55

Closed
bondarevts wants to merge 1 commit into
yaml:masterfrom
bondarevts:master
Closed

Improve RepresenterError creation#55
bondarevts wants to merge 1 commit into
yaml:masterfrom
bondarevts:master

Conversation

@bondarevts
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

This is excellent @bondarevts! Thanks!

@sigmavirus24
Copy link
Copy Markdown
Contributor

This was rebased and merged in ef744d8. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants