1. Kirill Simonov
  2. pyyaml
  3. Issues
Issue #5 new

pyyaml handles numpy numbers badly.

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 representer.py:57 (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