Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1666

Error message when trying to serialise unsupported types is unhelpful

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.7.2
    • Component/s: API, BSON, Error Handling
    • None
    • Environment:
      Any

      When trying to serialise an unsupported object type to bson, the error message that pymongo generates is sometimes very unhelpful.

       

      For example, I was trying to save a document like this:

      {
        "value": number1 < number2
      }
      

      Normally this would result in a bool, which would be fine.

      However, this is the error message I get when trying to run this:

      InvalidDocument: Cannot encode object: True
      

      You may ask why it cannot serialise a bool.

      It turns out that number1 can sometimes be a numpy type. As a result, the type of the comparison is actually `numpy.bool_` instead of a standard python bool.

       

      The error message would be much more helpful if it contained the type() of the object. For example:

      InvalidDocument: Cannot encode object of type 'numpy.bool_': True
      

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            acroock@isaziconsulting.co.za Ari Croock
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: