diff --git a/doc/changelog.rst b/doc/changelog.rst index 18b0160103..49141a8a18 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,19 @@ Changelog ========= +Changes in Version 4.20.2 (unreleased) +-------------------------------------- + +Bug fixes +......... + +- Fixed a bug where :meth:`~gridfs.GridFS.get_version` and + :meth:`~gridfs.GridFS.get_last_version` accepted a non-string ``filename`` + and treated a dict as query criteria. They now raise :exc:`TypeError` + (`PYTHON-6183`_). + +.. _PYTHON-6183: https://jira.mongodb.org/browse/PYTHON-6183 + Changes in Version 4.20.1 (2026/11/19) -------------------------------------- diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index 748092ffd3..095c50f7d3 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -47,7 +47,7 @@ from pymongo.asynchronous.cursor import AsyncCursor from pymongo.asynchronous.database import AsyncDatabase from pymongo.asynchronous.helpers import anext -from pymongo.common import validate_string +from pymongo.common import validate_string, validate_string_or_none from pymongo.errors import ( BulkWriteError, ConfigurationError, @@ -205,6 +205,8 @@ async def get_version( ``-3``, version ``1`` is the same as version ``-2``, and version ``2`` is the same as version ``-1``. + Raises :class:`TypeError` if `filename` is not a string or `None`. + Raises :class:`~gridfs.errors.NoFile` if no such version of that file exists. @@ -215,12 +217,16 @@ async def get_version( :class:`~pymongo.client_session.AsyncClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.2 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. .. versionchanged:: 3.1 ``get_version`` no longer ensures indexes. """ + validate_string_or_none("filename", filename) query = kwargs if filename is not None: query["filename"] = filename @@ -252,11 +258,16 @@ async def get_last_version( Equivalent to calling :meth:`get_version` with the default `version` (``-1``). + Raises :class:`TypeError` if `filename` is not a string or `None`. + :param filename: ``"filename"`` of the file to get, or `None` :param session: a :class:`~pymongo.client_session.AsyncClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.2 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. """ diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index e3d57fb0af..6d106fda36 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -43,7 +43,7 @@ _grid_out_property, ) from pymongo import ASCENDING, DESCENDING, WriteConcern, _csot -from pymongo.common import validate_string +from pymongo.common import validate_string, validate_string_or_none from pymongo.errors import ( BulkWriteError, ConfigurationError, @@ -205,6 +205,8 @@ def get_version( ``-3``, version ``1`` is the same as version ``-2``, and version ``2`` is the same as version ``-1``. + Raises :class:`TypeError` if `filename` is not a string or `None`. + Raises :class:`~gridfs.errors.NoFile` if no such version of that file exists. @@ -215,12 +217,16 @@ def get_version( :class:`~pymongo.client_session.ClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.2 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. .. versionchanged:: 3.1 ``get_version`` no longer ensures indexes. """ + validate_string_or_none("filename", filename) query = kwargs if filename is not None: query["filename"] = filename @@ -252,11 +258,16 @@ def get_last_version( Equivalent to calling :meth:`get_version` with the default `version` (``-1``). + Raises :class:`TypeError` if `filename` is not a string or `None`. + :param filename: ``"filename"`` of the file to get, or `None` :param session: a :class:`~pymongo.client_session.ClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.2 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. """ diff --git a/test/asynchronous/test_gridfs.py b/test/asynchronous/test_gridfs.py index 6b1c1c9460..e21d7a93a3 100644 --- a/test/asynchronous/test_gridfs.py +++ b/test/asynchronous/test_gridfs.py @@ -363,6 +363,13 @@ async def test_get_version_with_metadata(self): await self.fs.delete(two) await self.fs.delete(three) + async def test_get_version_invalid_filename(self): + msg = "Wrong type for filename" + with self.assertRaisesRegex(TypeError, msg): + await self.fs.get_version({"filename": "test"}) # type: ignore[arg-type] + with self.assertRaisesRegex(TypeError, msg): + await self.fs.get_last_version(filename=["test"]) # type: ignore[arg-type] + async def test_put_filelike(self): oid = await self.fs.put(BytesIO(b"hello world"), chunk_size=1) self.assertEqual(11, await self.db.fs.chunks.count_documents({})) diff --git a/test/test_gridfs.py b/test/test_gridfs.py index 6371fd2019..7173ee60f7 100644 --- a/test/test_gridfs.py +++ b/test/test_gridfs.py @@ -357,6 +357,13 @@ def test_get_version_with_metadata(self): self.fs.delete(two) self.fs.delete(three) + def test_get_version_invalid_filename(self): + msg = "Wrong type for filename" + with self.assertRaisesRegex(TypeError, msg): + self.fs.get_version({"filename": "test"}) # type: ignore[arg-type] + with self.assertRaisesRegex(TypeError, msg): + self.fs.get_last_version(filename=["test"]) # type: ignore[arg-type] + def test_put_filelike(self): oid = self.fs.put(BytesIO(b"hello world"), chunk_size=1) self.assertEqual(11, self.db.fs.chunks.count_documents({}))