From 3731372752d2cfd808f5bc300bfbcb6bd0bb7925 Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Wed, 18 Nov 2026 20:37:09 -0500 Subject: [PATCH 1/5] PYTHON-6183 Reject non-string filename in GridFS get_version --- doc/changelog.rst | 4 ++++ gridfs/asynchronous/grid_file.py | 3 ++- gridfs/synchronous/grid_file.py | 3 ++- test/asynchronous/test_gridfs.py | 11 +++++++++++ test/test_gridfs.py | 11 +++++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index d4e6193715..a590d7851a 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -12,7 +12,11 @@ Bug fixes - Fixed a bug where ``getMore`` commands in load-balanced mode were not retried when the server returned a retryable overload error (`PYTHON-6262`_). +- GridFS ``get_version`` and ``get_last_version`` now raise ``TypeError`` when + ``filename`` is not a string or ``None``, instead of treating a dict as query + criteria (`PYTHON-6183`_). +.. _PYTHON-6183: https://jira.mongodb.org/browse/PYTHON-6183 .. _PYTHON-6262: https://jira.mongodb.org/browse/PYTHON-6262 Changes in Version 4.20.0 (2026/11/12) diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index ef55d0c048..fdff4d422c 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, @@ -221,6 +221,7 @@ async def get_version( .. 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 diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index 0726f1c9bf..51aae229ad 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, @@ -221,6 +221,7 @@ def get_version( .. 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 diff --git a/test/asynchronous/test_gridfs.py b/test/asynchronous/test_gridfs.py index 6b1c1c9460..32fb745347 100644 --- a/test/asynchronous/test_gridfs.py +++ b/test/asynchronous/test_gridfs.py @@ -363,6 +363,17 @@ 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_version(filename={"filename": "test"}) # type: ignore[arg-type] + with self.assertRaisesRegex(TypeError, msg): + await self.fs.get_last_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..a29f46e992 100644 --- a/test/test_gridfs.py +++ b/test/test_gridfs.py @@ -357,6 +357,17 @@ 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_version(filename={"filename": "test"}) # type: ignore[arg-type] + with self.assertRaisesRegex(TypeError, msg): + self.fs.get_last_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({})) From 532fd7939e1a6e4663792293b6eb123a575e354e Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Wed, 18 Nov 2026 20:42:26 -0500 Subject: [PATCH 2/5] PYTHON-6183 Trim redundant GridFS version tests --- test/asynchronous/test_gridfs.py | 4 ---- test/test_gridfs.py | 4 ---- 2 files changed, 8 deletions(-) diff --git a/test/asynchronous/test_gridfs.py b/test/asynchronous/test_gridfs.py index 32fb745347..e21d7a93a3 100644 --- a/test/asynchronous/test_gridfs.py +++ b/test/asynchronous/test_gridfs.py @@ -367,10 +367,6 @@ 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_version(filename={"filename": "test"}) # type: ignore[arg-type] - with self.assertRaisesRegex(TypeError, msg): - await self.fs.get_last_version({"filename": "test"}) # type: ignore[arg-type] with self.assertRaisesRegex(TypeError, msg): await self.fs.get_last_version(filename=["test"]) # type: ignore[arg-type] diff --git a/test/test_gridfs.py b/test/test_gridfs.py index a29f46e992..7173ee60f7 100644 --- a/test/test_gridfs.py +++ b/test/test_gridfs.py @@ -361,10 +361,6 @@ 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_version(filename={"filename": "test"}) # type: ignore[arg-type] - with self.assertRaisesRegex(TypeError, msg): - self.fs.get_last_version({"filename": "test"}) # type: ignore[arg-type] with self.assertRaisesRegex(TypeError, msg): self.fs.get_last_version(filename=["test"]) # type: ignore[arg-type] From 653892ca50a6c1119dc8398228235c1f52bb7f87 Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Wed, 18 Nov 2026 20:48:24 -0500 Subject: [PATCH 3/5] PYTHON-6183 Follow changelog format rules --- doc/changelog.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index a590d7851a..2680711a2f 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -12,9 +12,10 @@ Bug fixes - Fixed a bug where ``getMore`` commands in load-balanced mode were not retried when the server returned a retryable overload error (`PYTHON-6262`_). -- GridFS ``get_version`` and ``get_last_version`` now raise ``TypeError`` when - ``filename`` is not a string or ``None``, instead of treating a dict as query - criteria (`PYTHON-6183`_). +- 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 .. _PYTHON-6262: https://jira.mongodb.org/browse/PYTHON-6262 From efa59a7ac131e0913ff98e1b66222f3e23799e11 Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Thu, 19 Nov 2026 12:05:06 -0500 Subject: [PATCH 4/5] PYTHON-6183 Address review: document TypeError and reorder changelog links --- doc/changelog.rst | 2 +- gridfs/asynchronous/grid_file.py | 7 +++++++ gridfs/synchronous/grid_file.py | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 2680711a2f..a6828a03ff 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -17,8 +17,8 @@ Bug fixes and treated a dict as query criteria. They now raise :exc:`TypeError` (`PYTHON-6183`_). -.. _PYTHON-6183: https://jira.mongodb.org/browse/PYTHON-6183 .. _PYTHON-6262: https://jira.mongodb.org/browse/PYTHON-6262 +.. _PYTHON-6183: https://jira.mongodb.org/browse/PYTHON-6183 Changes in Version 4.20.0 (2026/11/12) -------------------------------------- diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index fdff4d422c..717e8d31b9 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -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,6 +217,9 @@ async def get_version( :class:`~pymongo.client_session.AsyncClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.1 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. @@ -253,6 +258,8 @@ 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` diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index 51aae229ad..6632f6cdb1 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -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,6 +217,9 @@ def get_version( :class:`~pymongo.client_session.ClientSession` :param kwargs: find files by custom metadata. + .. versionchanged:: 4.20.1 + Raise :class:`TypeError` if `filename` is not a string or `None`. + .. versionchanged:: 3.6 Added ``session`` parameter. @@ -253,6 +258,8 @@ 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` From 4a8a15aefc2e3dad13131af01bc922bde60d7941 Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Thu, 19 Nov 2026 18:04:37 -0500 Subject: [PATCH 5/5] PYTHON-6183 Target 4.20.2 and document get_last_version change --- doc/changelog.rst | 18 +++++++++++++----- gridfs/asynchronous/grid_file.py | 5 ++++- gridfs/synchronous/grid_file.py | 5 ++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 00e87f92cc..2af318cdb2 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) -------------------------------------- @@ -12,10 +25,6 @@ Bug fixes - Fixed a bug where ``getMore`` commands in load-balanced mode were not retried when the server returned a retryable overload error (`PYTHON-6262`_). -- 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`_). - Fixed a bug where a cancelled async command published a ``CommandStartedEvent`` (and its "Command started" log message) without a correlating ``CommandFailedEvent``. A cancellation now publishes the failure @@ -24,7 +33,6 @@ Bug fixes - Improved credential validation. (`PYTHON-6073`_) .. _PYTHON-6262: https://jira.mongodb.org/browse/PYTHON-6262 -.. _PYTHON-6183: https://jira.mongodb.org/browse/PYTHON-6183 .. _PYTHON-6187: https://jira.mongodb.org/browse/PYTHON-6187 .. _PYTHON-6058: https://jira.mongodb.org/browse/PYTHON-6058 .. _PYTHON-6073: https://jira.mongodb.org/browse/PYTHON-6073 diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index 3e2e5f77d5..095c50f7d3 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -217,7 +217,7 @@ async def get_version( :class:`~pymongo.client_session.AsyncClientSession` :param kwargs: find files by custom metadata. - .. versionchanged:: 4.20.1 + .. versionchanged:: 4.20.2 Raise :class:`TypeError` if `filename` is not a string or `None`. .. versionchanged:: 3.6 @@ -265,6 +265,9 @@ async def get_last_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. """ diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index 65732753c4..6d106fda36 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -217,7 +217,7 @@ def get_version( :class:`~pymongo.client_session.ClientSession` :param kwargs: find files by custom metadata. - .. versionchanged:: 4.20.1 + .. versionchanged:: 4.20.2 Raise :class:`TypeError` if `filename` is not a string or `None`. .. versionchanged:: 3.6 @@ -265,6 +265,9 @@ def get_last_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. """