Skip to content

Commit 5da08ae

Browse files
authored
Fix suppression of warnings for internal API usage (#1040)
Using `warnings.catch_warning` for suppressing warnings when using APIs internally that are preview/experimental/deprecated is a bad idea because the `warnings` module stores the configuration globally per module. This is not thread-safe. Therefore, the code was restructured to not rely of the `warnings` module to suppress those warnings. Instead, warning-free internal APIs are being used.
1 parent 06f356b commit 5da08ae

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

neo4j/conf.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ def _consume(cls, data):
150150
config[key] = value
151151
return cls(config)
152152

153-
def __update(self, data):
153+
def __update(self, data, warn=True):
154154
data_dict = dict(iter_items(data))
155155

156156
def set_attr(k, v):
157157
if k in self.keys():
158-
if k in self._deprecated_options():
158+
if warn and k in self._deprecated_options():
159159
deprecation_warn("The '{}' config key is "
160160
"deprecated".format(k))
161161
setattr(self, k, v)
@@ -164,10 +164,11 @@ def set_attr(k, v):
164164
if k0 in data_dict:
165165
raise ValueError("Cannot specify both '{}' and '{}' in "
166166
"config".format(k0, k))
167-
deprecation_warn(
168-
"The '{}' config key is deprecated, please use "
169-
"'{}' instead".format(k, k0)
170-
)
167+
if warn:
168+
deprecation_warn(
169+
"The '{}' config key is deprecated, please use "
170+
"'{}' instead".format(k, k0)
171+
)
171172
set_attr(k0, v)
172173
else:
173174
raise AttributeError(k)
@@ -189,10 +190,7 @@ def set_attr(k, v):
189190
def __init__(self, *args, **kwargs):
190191
for arg in args:
191192
if isinstance(arg, Config):
192-
with warnings.catch_warnings():
193-
warnings.filterwarnings("ignore",
194-
category=DeprecationWarning)
195-
self.__update(arg)
193+
self.__update(arg, warn=False)
196194
else:
197195
self.__update(arg)
198196
self.__update(kwargs)

0 commit comments

Comments
 (0)