Skip to content

Commit 8f7b86f

Browse files
authored
PYTHON-4057 Emit DeprecationWarning for deprecated GridFS apis (#1593)
1 parent 09e24a4 commit 8f7b86f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

gridfs/grid_file.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io
2020
import math
2121
import os
22+
import warnings
2223
from typing import Any, Iterable, Mapping, NoReturn, Optional
2324

2425
from bson.int64 import Int64
@@ -69,8 +70,15 @@ def _grid_in_property(
6970
closed_only: Optional[bool] = False,
7071
) -> Any:
7172
"""Create a GridIn property."""
73+
warn_str = ""
74+
if docstring.startswith("DEPRECATED,"):
75+
warn_str = (
76+
f"GridIn property '{field_name}' is deprecated and will be removed in PyMongo 5.0"
77+
)
7278

7379
def getter(self: Any) -> Any:
80+
if warn_str:
81+
warnings.warn(warn_str, stacklevel=2, category=DeprecationWarning)
7482
if closed_only and not self._closed:
7583
raise AttributeError("can only get %r on a closed file" % field_name)
7684
# Protect against PHP-237
@@ -79,6 +87,8 @@ def getter(self: Any) -> Any:
7987
return self._file.get(field_name, None)
8088

8189
def setter(self: Any, value: Any) -> Any:
90+
if warn_str:
91+
warnings.warn(warn_str, stacklevel=2, category=DeprecationWarning)
8292
if self._closed:
8393
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {field_name: value}})
8494
self._file[field_name] = value
@@ -100,8 +110,15 @@ def setter(self: Any, value: Any) -> Any:
100110

101111
def _grid_out_property(field_name: str, docstring: str) -> Any:
102112
"""Create a GridOut property."""
113+
warn_str = ""
114+
if docstring.startswith("DEPRECATED,"):
115+
warn_str = (
116+
f"GridOut property '{field_name}' is deprecated and will be removed in PyMongo 5.0"
117+
)
103118

104119
def getter(self: Any) -> Any:
120+
if warn_str:
121+
warnings.warn(warn_str, stacklevel=2, category=DeprecationWarning)
105122
self._ensure_file()
106123

107124
# Protect against PHP-237

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ filterwarnings = [
115115
"module:Unsupported compressor:UserWarning",
116116
"module:zlibcompressionlevel must be:UserWarning",
117117
"module:Wire protocol compression with:UserWarning",
118+
"module:GridIn property:DeprecationWarning",
119+
"module:GridOut property:DeprecationWarning",
118120
# TODO: Remove as part of PYTHON-3923.
119121
"module:unclosed <eventlet.green.ssl.GreenSSLSocket:ResourceWarning",
120122
"module:unclosed <socket.socket:ResourceWarning",

0 commit comments

Comments
 (0)