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

Add __bytes__ to ObjectId to support bytes(oid) in Python 3

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: BSON
    • Labels:
      None

      Description

      We could add __bytes__ to ObjectId to support bytes(oid) in Python 3. Currently this is not supported:

      >>> bytes(ObjectId())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: cannot convert 'ObjectId' object to bytes
      

      The implementation can just be an alias for the binary property:

      class ObjectId(object):
      ...
          def __bytes__(self):
              return self.binary
      

      This was requested in PYTHON-1843.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              shane.harvey Shane Harvey
              Participants:
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: