-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Unknown
-
Affects Version/s: 4.15.0, 4.15.1, 4.15.2
-
Component/s: Typing
-
🔵 Done
-
Python Drivers
-
Not Needed
-
-
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
- is caused by
-
PYTHON-5257 Type hints for "let" are missing generic param for Mapping
-
- Closed
-