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

Make MongoClient, Database and Collection objects hashable

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.12, 4.0
    • Affects Version/s: 3.11.2
    • Component/s: None
    • None
    • Environment:
      Python 3.8
    • Fully Compatible

      I'd like to make the Database and Collection objects hashable so that I can use them easily in caching mechanism.

      For instance:

      @functools.lru_cache
      def get_governor(database: pymongo.database.Database, state: str) -> Optional[str]:
       officials = database.officials.find_one({'_id': state})
       if not officials:
       return None
       return officials.get('governor')
      

      Today this is breaking because database is not hashable. It's not hashable because it has an _eq_ method but no _hash_ method.

       

       

            Assignee:
            prashant.mital Prashant Mital (Inactive)
            Reporter:
            pascal@bayesimpact.org Pascal Corpet
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: