distinct return type incorrectly specified as list[str]

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • 4.15.3
    • Affects Version/s: 4.15.0, 4.15.1, 4.15.2
    • Component/s: Typing
    • 🔵 Done
    • Python Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      PYTHON-5257 (https://github.com/mongodb/mongo-python-driver/pull/2456) updated several return types.

      However, distinct methods were incorrectly given a return type of list[str], when any type used in the collection for the field may be returned.

      Detailed steps to reproduce the problem?

      Include full traceback, if possible

       

      Example:

      import bson
      import pymongo

      def setup() -> pymongo.collection.Collection:
          client = pymongo.MongoClient()
          db = client.test_database
          collection = db.test_collection
          collection.insert_many(
              [
                  {"_id": None},
                  {"_id": 0},
                  {"_id": ""},
                  {"_id": bson.ObjectId()},
                  {"_id": True},
              ]
          )
          return collection

      def collection_distinct(
          collection: pymongo.collection.Collection,
      ) -> list[None | int | str | bson.ObjectId | bool]:
          return collection.distinct("_id")

      def cursor_distinct(
          collection: pymongo.collection.Collection,
      ) -> list[None | int | str | bson.ObjectId | bool]:
          cursor = collection.find()
          return cursor.distinct("_id")

      if _name_ == "_main_":
          coll = setup()
          print("Collection distinct:", collection_distinct(coll))
          print("Cursor distinct:", cursor_distinct(coll))
          # coll.delete_many({})

       

      $ mypy example.py

      example.py:24: error: Incompatible return value type (got "list[str]", expected
      "list[int | str | ObjectId | bool | None]")  [return-value]
              return collection.distinct("_id")
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
      example.py:24: note: "list" is invariant – see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
      example.py:24: note: Consider using "Sequence" instead, which is covariant
      example.py:31: error: Incompatible return value type (got "list[str]", expected
      "list[int | str | ObjectId | bool | None]")  [return-value]
              return cursor.distinct("_id")
                     ^~~~~~~~~~~~~~~~~~~~~~
      example.py:31: note: "list" is invariant – see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
      example.py:31: note: Consider using "Sequence" instead, which is covariant
      Found 2 errors in 1 file (checked 1 source file)

      Definition of done: what must be done to consider the task complete?

      Generalize return types of all the distinct methods, most likely to list[Any].

      The exact Python version used, with patch level:

      $ python -c "import sys; print(sys.version)"

      3.12.8 (main, Dec 19 2024, 14:22:58) [Clang 18.1.8 ]

      The exact version of PyMongo used, with patch level:

      $ python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"

      4.15.0
      True

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      local

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      OSX 15.7

      Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)

      Security Vulnerabilities

      If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here

            Assignee:
            Steve Silvester
            Reporter:
            Adam Davis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: