Uploaded image for project: 'Motor'
  1. Motor
  2. MOTOR-1279

Inconsistent type annotations for client.get_database() and client["database"]

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.4
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Python Drivers

      The types returned by client.get_database() and client[database] are identical. The type annotations are inconsistent: client.get_database() is annotated as Database where client[database] is annotated as AgnosticDatabase. Using get_database() will therefore produce incorrect warnings from mypy.

      Instead it should also by annotated to return an AgnosticDatabase so that mypy produces correct results.

      from motor.motor_asyncio import AsyncIOMotorClient
      
      MONGODB_URI="mongodb+srv://username:password@hostname/database?retryWrites=true&w=majority&appName=test"
      client = AsyncIOMotorClient(MONGODB_URI)
      db=client.get_database()  # preferred way, allows to configure database name without extra code
      db=client["database"]  # suboptimal way, requires extra code, produces correct type annotations
      

       

      Motor version is 3.3.2.

        1. Screenshot 2024-03-12 at 20.50.02.png
          26 kB
          Till Backhaus
        2. Screenshot 2024-03-12 at 20.50.16.png
          35 kB
          Till Backhaus

            Assignee:
            steve.silvester@mongodb.com Steve Silvester
            Reporter:
            till@legalos.io Till Backhaus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: