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

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

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: