|
| 1 | +import warnings |
| 2 | +from functools import wraps |
| 3 | + |
1 | 4 | import numpy as np
|
2 | 5 | import pytest
|
3 | 6 |
|
|
7 | 10 | convert_bytes,
|
8 | 11 | make_dict,
|
9 | 12 | )
|
10 |
| -from redisvl.utils.utils import deprecated_argument |
| 13 | +from redisvl.utils.utils import assert_no_warnings, deprecated_argument |
11 | 14 |
|
12 | 15 |
|
13 | 16 | def test_even_number_of_elements():
|
@@ -156,92 +159,264 @@ def test_conversion_with_invalid_floats():
|
156 | 159 | assert len(result) > 0 # Simple check to ensure it returns anything
|
157 | 160 |
|
158 | 161 |
|
| 162 | +def decorator(func): |
| 163 | + @wraps(func) |
| 164 | + def wrapper(*args, **kwargs): |
| 165 | + print("boop") |
| 166 | + return func(*args, **kwargs) |
| 167 | + |
| 168 | + return wrapper |
| 169 | + |
| 170 | + |
159 | 171 | class TestDeprecatedArgument:
|
160 | 172 | def test_deprecation_warning_text_with_replacement(self):
|
161 |
| - @deprecated_argument("dtype", "vectorizer") |
162 |
| - def test_func(dtype=None, vectorizer=None): |
| 173 | + @deprecated_argument("old_arg", "new_arg") |
| 174 | + def test_func(old_arg=None, new_arg=None, neutral_arg=None): |
163 | 175 | pass
|
164 | 176 |
|
| 177 | + # Test that passing the deprecated argument as a keyword triggers the warning. |
| 178 | + with pytest.warns(DeprecationWarning) as record: |
| 179 | + test_func(old_arg="float32") |
| 180 | + |
| 181 | + assert len(record) == 1 |
| 182 | + assert str(record[0].message) == ( |
| 183 | + "Argument old_arg is deprecated and will be removed in the next major release. Use new_arg instead." |
| 184 | + ) |
| 185 | + |
| 186 | + # Test that passing the deprecated argument as a positional argument also triggers the warning. |
165 | 187 | with pytest.warns(DeprecationWarning) as record:
|
166 |
| - test_func(dtype="float32") |
| 188 | + test_func("float32", neutral_arg="test_vector") |
167 | 189 |
|
168 | 190 | assert len(record) == 1
|
169 | 191 | assert str(record[0].message) == (
|
170 |
| - "Argument dtype is deprecated and will be removed" |
171 |
| - " in the next major release. Use vectorizer instead." |
| 192 | + "Argument old_arg is deprecated and will be removed in the next major release. Use new_arg instead." |
172 | 193 | )
|
173 | 194 |
|
| 195 | + with assert_no_warnings(): |
| 196 | + test_func(new_arg="float32") |
| 197 | + test_func(new_arg="float32", neutral_arg="test_vector") |
| 198 | + |
174 | 199 | def test_deprecation_warning_text_without_replacement(self):
|
175 |
| - @deprecated_argument("dtype") |
176 |
| - def test_func(dtype=None): |
| 200 | + @deprecated_argument("old_arg") |
| 201 | + def test_func(old_arg=None, neutral_arg=None): |
177 | 202 | pass
|
178 | 203 |
|
| 204 | + # As a kwarg |
179 | 205 | with pytest.warns(DeprecationWarning) as record:
|
180 |
| - test_func(dtype="float32") |
| 206 | + test_func(old_arg="float32") |
181 | 207 |
|
182 | 208 | assert len(record) == 1
|
183 | 209 | assert str(record[0].message) == (
|
184 |
| - "Argument dtype is deprecated and will be removed" |
| 210 | + "Argument old_arg is deprecated and will be removed" |
185 | 211 | " in the next major release."
|
186 | 212 | )
|
187 | 213 |
|
188 |
| - def test_function_argument(self): |
189 |
| - @deprecated_argument("dtype", "vectorizer") |
190 |
| - def test_func(dtype=None, vectorizer=None): |
| 214 | + # As a positional arg |
| 215 | + with pytest.warns(DeprecationWarning): |
| 216 | + test_func("float32", neutral_arg="test_vector") |
| 217 | + |
| 218 | + assert len(record) == 1 |
| 219 | + assert str(record[0].message) == ( |
| 220 | + "Argument old_arg is deprecated and will be removed" |
| 221 | + " in the next major release." |
| 222 | + ) |
| 223 | + |
| 224 | + with assert_no_warnings(): |
| 225 | + test_func(neutral_arg="test_vector") |
| 226 | + |
| 227 | + def test_function_positional_argument_required(self): |
| 228 | + """ |
| 229 | + NOTE: In this situation, it's not possible to avoid a deprecation |
| 230 | + warning because the argument is currently required. |
| 231 | + """ |
| 232 | + |
| 233 | + @deprecated_argument("old_arg") |
| 234 | + def test_func(old_arg, neutral_arg): |
| 235 | + pass |
| 236 | + |
| 237 | + with pytest.warns(DeprecationWarning): |
| 238 | + test_func("float32", "bob") |
| 239 | + |
| 240 | + def test_function_positional_argument_optional(self): |
| 241 | + @deprecated_argument("old_arg") |
| 242 | + def test_func(neutral_arg, old_arg=None): |
191 | 243 | pass
|
192 | 244 |
|
193 | 245 | with pytest.warns(DeprecationWarning):
|
194 |
| - test_func(dtype="float32") |
| 246 | + test_func("bob", "float32") |
| 247 | + |
| 248 | + with assert_no_warnings(): |
| 249 | + test_func("bob") |
195 | 250 |
|
196 | 251 | def test_function_keyword_argument(self):
|
197 |
| - @deprecated_argument("dtype", "vectorizer") |
198 |
| - def test_func(dtype=None, vectorizer=None): |
| 252 | + @deprecated_argument("old_arg", "new_arg") |
| 253 | + def test_func(old_arg=None, new_arg=None): |
| 254 | + pass |
| 255 | + |
| 256 | + with pytest.warns(DeprecationWarning): |
| 257 | + test_func(old_arg="float32") |
| 258 | + |
| 259 | + with assert_no_warnings(): |
| 260 | + test_func(new_arg="float32") |
| 261 | + |
| 262 | + def test_function_keyword_argument_multiple_decorators(self): |
| 263 | + @deprecated_argument("old_arg", "new_arg") |
| 264 | + @decorator |
| 265 | + def test_func(old_arg=None, new_arg=None): |
199 | 266 | pass
|
200 | 267 |
|
201 | 268 | with pytest.warns(DeprecationWarning):
|
202 |
| - test_func(vectorizer="float32") |
| 269 | + test_func(old_arg="float32") |
| 270 | + |
| 271 | + with assert_no_warnings(): |
| 272 | + test_func(new_arg="float32") |
| 273 | + |
| 274 | + def test_method_positional_argument_optional(self): |
| 275 | + class TestClass: |
| 276 | + @deprecated_argument("old_arg", "new_arg") |
| 277 | + def test_method(self, new_arg=None, old_arg=None): |
| 278 | + pass |
| 279 | + |
| 280 | + with pytest.warns(DeprecationWarning): |
| 281 | + TestClass().test_method("float32", "bob") |
| 282 | + |
| 283 | + with assert_no_warnings(): |
| 284 | + TestClass().test_method("float32") |
| 285 | + |
| 286 | + def test_method_positional_argument_required(self): |
| 287 | + """ |
| 288 | + NOTE: In this situation, it's not possible to avoid a deprecation |
| 289 | + warning because the argument is currently required. |
| 290 | + """ |
| 291 | + |
| 292 | + class TestClass: |
| 293 | + @deprecated_argument("old_arg", "new_arg") |
| 294 | + def test_method(self, old_arg, new_arg): |
| 295 | + pass |
| 296 | + |
| 297 | + with pytest.warns(DeprecationWarning): |
| 298 | + TestClass().test_method("float32", new_arg="bob") |
203 | 299 |
|
204 |
| - def test_class_method_argument(self): |
| 300 | + def test_method_keyword_argument(self): |
205 | 301 | class TestClass:
|
206 |
| - @deprecated_argument("dtype", "vectorizer") |
207 |
| - def test_method(self, dtype=None, vectorizer=None): |
| 302 | + @deprecated_argument("old_arg", "new_arg") |
| 303 | + def test_method(self, old_arg=None, new_arg=None): |
208 | 304 | pass
|
209 | 305 |
|
210 | 306 | with pytest.warns(DeprecationWarning):
|
211 |
| - TestClass().test_method(dtype="float32") |
| 307 | + TestClass().test_method(old_arg="float32") |
| 308 | + |
| 309 | + with assert_no_warnings(): |
| 310 | + TestClass().test_method(new_arg="float32") |
| 311 | + |
| 312 | + def test_classmethod_positional_argument_required(self): |
| 313 | + """ |
| 314 | + NOTE: In this situation, it's impossible to avoid a deprecation |
| 315 | + warning because the argument is currently required. |
| 316 | + """ |
| 317 | + |
| 318 | + class TestClass: |
| 319 | + @deprecated_argument("old_arg", "new_arg") |
| 320 | + @classmethod |
| 321 | + def test_method(cls, old_arg, new_arg): |
| 322 | + pass |
| 323 | + |
| 324 | + with pytest.warns(DeprecationWarning): |
| 325 | + TestClass.test_method("float32", new_arg="bob") |
| 326 | + |
| 327 | + def test_classmethod_positional_argument_optional(self): |
| 328 | + class TestClass: |
| 329 | + @deprecated_argument("old_arg", "new_arg") |
| 330 | + @classmethod |
| 331 | + def test_method(cls, new_arg=None, old_arg=None): |
| 332 | + pass |
| 333 | + |
| 334 | + with pytest.warns(DeprecationWarning): |
| 335 | + TestClass.test_method("float32", "bob") |
| 336 | + |
| 337 | + with assert_no_warnings(): |
| 338 | + TestClass.test_method("float32") |
212 | 339 |
|
213 |
| - def test_class_method_keyword_argument(self): |
| 340 | + def test_classmethod_keyword_argument(self): |
214 | 341 | class TestClass:
|
215 |
| - @deprecated_argument("dtype", "vectorizer") |
216 |
| - def test_method(self, dtype=None, vectorizer=None): |
| 342 | + @deprecated_argument("old_arg", "new_arg") |
| 343 | + @classmethod |
| 344 | + def test_method(cls, old_arg=None, new_arg=None): |
217 | 345 | pass
|
218 | 346 |
|
219 | 347 | with pytest.warns(DeprecationWarning):
|
220 |
| - TestClass().test_method(vectorizer="float32") |
| 348 | + TestClass.test_method(old_arg="float32") |
| 349 | + |
| 350 | + with assert_no_warnings(): |
| 351 | + TestClass.test_method(new_arg="float32") |
| 352 | + |
| 353 | + def test_classmethod_keyword_argument_multiple_decorators(self): |
| 354 | + """ |
| 355 | + NOTE: The @deprecated_argument decorator should come between @classmethod |
| 356 | + and the method definition. |
| 357 | + """ |
| 358 | + |
| 359 | + class TestClass: |
| 360 | + @classmethod |
| 361 | + @deprecated_argument("old_arg", "new_arg") |
| 362 | + @decorator |
| 363 | + def test_method(cls, old_arg=None, new_arg=None): |
| 364 | + pass |
| 365 | + |
| 366 | + with pytest.warns(DeprecationWarning): |
| 367 | + TestClass.test_method(old_arg="float32") |
| 368 | + |
| 369 | + with assert_no_warnings(): |
| 370 | + TestClass.test_method(new_arg="float32") |
221 | 371 |
|
222 | 372 | def test_class_init_argument(self):
|
223 | 373 | class TestClass:
|
224 |
| - @deprecated_argument("dtype", "vectorizer") |
225 |
| - def __init__(self, dtype=None, vectorizer=None): |
| 374 | + @deprecated_argument("old_arg", "new_arg") |
| 375 | + def __init__(self, old_arg=None, new_arg=None): |
226 | 376 | pass
|
227 | 377 |
|
228 | 378 | with pytest.warns(DeprecationWarning):
|
229 |
| - TestClass(dtype="float32") |
| 379 | + TestClass(old_arg="float32") |
230 | 380 |
|
231 | 381 | def test_class_init_keyword_argument(self):
|
232 | 382 | class TestClass:
|
233 |
| - @deprecated_argument("dtype", "vectorizer") |
234 |
| - def __init__(self, dtype=None, vectorizer=None): |
| 383 | + @deprecated_argument("old_arg", "new_arg") |
| 384 | + def __init__(self, old_arg=None, new_arg=None): |
| 385 | + pass |
| 386 | + |
| 387 | + with pytest.warns(DeprecationWarning): |
| 388 | + TestClass(old_arg="float32") |
| 389 | + |
| 390 | + with assert_no_warnings(): |
| 391 | + TestClass(new_arg="float32") |
| 392 | + |
| 393 | + def test_class_init_keyword_argument_kwargs(self): |
| 394 | + class TestClass: |
| 395 | + @deprecated_argument("old_arg", "new_arg") |
| 396 | + def __init__(self, new_arg=None, **kwargs): |
235 | 397 | pass
|
236 | 398 |
|
237 | 399 | with pytest.warns(DeprecationWarning):
|
238 |
| - TestClass(dtype="float32") |
| 400 | + TestClass(old_arg="float32") |
| 401 | + |
| 402 | + with assert_no_warnings(): |
| 403 | + TestClass(new_arg="float32") |
239 | 404 |
|
240 | 405 | async def test_async_function_argument(self):
|
241 |
| - @deprecated_argument("dtype", "vectorizer") |
242 |
| - async def test_func(dtype=None, vectorizer=None): |
| 406 | + @deprecated_argument("old_arg", "new_arg") |
| 407 | + async def test_func(old_arg=None, new_arg=None): |
243 | 408 | return 1
|
244 | 409 |
|
245 | 410 | with pytest.warns(DeprecationWarning):
|
246 |
| - result = await test_func(dtype="float32") |
| 411 | + result = await test_func(old_arg="float32") |
247 | 412 | assert result == 1
|
| 413 | + |
| 414 | + async def test_ignores_local_variable(self): |
| 415 | + @deprecated_argument("old_arg", "new_arg") |
| 416 | + async def test_func(old_arg=None, new_arg=None): |
| 417 | + # The presence of this variable should not trigger a warning |
| 418 | + old_arg = "float32" |
| 419 | + return 1 |
| 420 | + |
| 421 | + with assert_no_warnings(): |
| 422 | + await test_func() |
0 commit comments