Skip to content
13 changes: 13 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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)
--------------------------------------

Expand Down
13 changes: 12 additions & 1 deletion gridfs/asynchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
13 changes: 12 additions & 1 deletion gridfs/synchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
7 changes: 7 additions & 0 deletions test/asynchronous/test_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({}))
Expand Down
7 changes: 7 additions & 0 deletions test/test_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({}))
Expand Down
Loading