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

MongoClient del raises TypeError: 'NoneType' object is not callable

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.11
    • Affects Version/s: 4.9, 4.10
    • Component/s: None
    • None
    • Python Drivers
    • Not Needed

      MongoClient del can raise TypeError: 'NoneType' object is not callable at interpreter shutdown:

      $ python
      Python 3.13.0 (v3.13.0:60403a5409f, Oct  7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> client = MongoClient()
      >>> exit()
      Exception ignored in: <function MongoClient.__del__ at 0x127bf02c0>
      Traceback (most recent call last):
        File "/Users/shane/git/mongo-python-driver/pymongo/synchronous/mongo_client.py", line 1192, in __del__
      TypeError: 'NoneType' object is not callable
      

      At interpreter shutdown global variables can be set to None so we have to guard against this case:

                  warn = warning.warn
                  cls = ResourceWarning
                  if self._opened and not self._closed and warn and cls:
                      warn(
                          (
                              f"Unclosed {type(self).__name__} opened at:\n{self._topology_settings._stack}"
                              f"Call {self.__class__.__name__}.close() to safely shut down your client and free up resources."
                          ),
                          cls,
                          stacklevel=2,
                      )
      

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

              Created:
              Updated:
              Resolved: