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

Clarify docs - ObjectId constructor with str vs with unicode

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Trivial - P5 Trivial - P5
    • 2.8, 3.0
    • Affects Version/s: 2.7.2
    • Component/s: None
    • Labels:
      None
    • Environment:
      Linux Mint 17, Python 2.7.6

      Example:

      >>> ObjectId('some.words..')
      ObjectId('736f6d652e776f7264696573')
      >>> ObjectId(u'some.words..')
      Traceback (most recent call last):
      [...]
      raise InvalidId("%s is not a valid ObjectId" % oid)
      bson.errors.InvalidId: some.wordies is not a valid ObjectId

      I'm not sure whether this would be considered a bug or whether it's strictly intentional, but from the point of view of an application developer, I find it very confusing that the ObjectId constructor doesn't handle `str` and `unicode` the same. I ran into it in using a library (Eve) which uses the constructor as a way to check whether a string can be an ObjectId or not. It was particularly mysterious because it only happens for strings of length 12, so it has the potential to cause bugs in applications which would go undetected by tests unless someone happened to pick a 12-letter test string (as I did).

      Thanks!

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            oneeman Or Neeman
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: