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

Improve error message TypeError: upsert must be True or False

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.4
    • Affects Version/s: 3.10.1
    • Component/s: Error Handling
    • Labels:

      Given the following code

       

      from pymongo import MongoClient
      import datetimeclient = MongoClient()
      collection = client.demo.data

      collection.drop()
      collection.update_one({ '_id' : 'stats'}, { '$set': { 'total': 1 }}, upsert=True)
      collection.update_one({ '_id' : 'stats'}, { '$set': { 'total': 2 }}, { "upsert": True })

       

      the first call to update_one works, the second raises an exception which is IMHO very confusing. It would be nice if the exception could be improved. Currently it says:

       

      TypeError: upsert must be True or False

       

      Full Traceback:

       

      Traceback (most recent call last):
      File "up.py", line 9, in <module>
      collection.update_one({ '_id' : 'stats'}, { '$set':

      Unknown macro: { 'total'}

      }, { "upsert": True })
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/collection.py", line 998, in update_one
      self._update_retryable(
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/collection.py", line 854, in _update_retryable
      return self.__database.client._retryable_write(
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1491, in _retryable_write
      return self._retry_with_session(retryable, func, s, None)
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1384, in _retry_with_session
      return func(session, sock_info, retryable)
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/collection.py", line 846, in _update
      return self._update(
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/collection.py", line 767, in _update
      common.validate_boolean("upsert", upsert)
      File "/home/gabor/venv3/lib/python3.8/site-packages/pymongo/common.py", line 159, in validate_boolean
      raise TypeError("%s must be True or False" % (option,))
      TypeError: upsert must be True or False

       

       

       

            Assignee:
            iris.ho@mongodb.com Iris Ho (Inactive)
            Reporter:
            gabor@szabgab.com Gabor Szabo
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: