Skip to content

Commit e92f07c

Browse files
authored
Merge pull request #2935 from bagerard/fix_deprecated_pymongo_contenttype2
Adapt for pymongo 5 deprecation warning for GridFs.content_type
2 parents 428b582 + 7175e5a commit e92f07c

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎mongoengine/fields.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,11 @@ def __getattr__(self, name):
17021702
return getattr(obj, name)
17031703
raise AttributeError
17041704

1705+
@property
1706+
def content_type(self):
1707+
# PyMongo deprecated content_type in favor of the GridFS contentType field.
1708+
return self.__getattr__("contentType")
1709+
17051710
def __get__(self, instance, value):
17061711
return self
17071712

‎tests/fields/test_file_field.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import tempfile
44
import unittest
5+
import warnings
56
from io import BytesIO
67

78
import gridfs
@@ -69,7 +70,9 @@ class PutFile(Document):
6970
== "<GridFSProxy: hello (%s)>" % result.the_file.grid_id
7071
)
7172
assert result.the_file.read() == text
72-
assert result.the_file.content_type == content_type
73+
with warnings.catch_warnings():
74+
warnings.simplefilter("error", DeprecationWarning)
75+
assert result.the_file.content_type == content_type
7376
result.the_file.delete() # Remove file from GridFS
7477
PutFile.objects.delete()
7578

0 commit comments

Comments
 (0)