pyyaml handles numpy numbers badly.

TomRitchford avatarTomRitchford created an issue

Hello, and thanks for an excellent package that I use almost every day!

I finally tracked down an intermittent error where yaml.safe_dump throws an exception from the method "represent_undefined":

ERROR: cannot represent an object: 1.0

which seemed ridiculous. And printing out what I was trying to dump and running it through pyyaml worked fine!

It turned out that some of the numbers are sometimes a numpy.float64. Now, this is a class derived from float, so these are also floats, but their base type is not float.

A quick bandaid solution would be at least to tell the user what the offending type was when you raise the exception in represent_undefined, rather than just the value.

But you should really not be coding based on the items that you're representing being exactly the classes you expect - you should use isinstance.

Most of the time all the elements won't be of a derived class, so you could keep all the logic leading up to (in the copy I'm looking up, not sure if it's totally up-to-date) and simply look again through all the representers using isinstance. The only nodes that will "pay the price" will either be ones that contain derived classes and need to be treated correctly, or ones that are actually wrong.

Comments (1)

  1. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.