summaryrefslogtreecommitdiff
path: root/test/test_deprecation.py
blob: 50a1c0cbb71517e7324788fd888fe94c0cebd7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-common.
#
# logilab-common is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option) any
# later version.
#
# logilab-common is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
"""unit tests for logilab.common.deprecation"""

import os
import warnings

from inspect import currentframe, getframeinfo

from logilab.common.testlib import TestCase, unittest_main
from logilab.common.modutils import LazyObject
from logilab.common import deprecation


CURRENT_FILE = os.path.abspath(__file__)


class RawInputTC(TestCase):

    # XXX with 2.6 we could test warnings
    # http://docs.python.org/library/warnings.html#testing-warnings
    # instead we just make sure it does not crash

    def mock_warn(self, *args, **kwargs):
        self.messages.append(str(args[0]))

    def setUp(self):
        self.messages = []
        deprecation.warn = self.mock_warn

    def tearDown(self):
        deprecation.warn = warnings.warn

    def mk_func(self):
        def any_func():
            pass

        return any_func

    def test_class_deprecated(self):
        class AnyClass(object, metaclass=deprecation.class_deprecated):
            pass

        AnyClass()
        self.assertEqual(self.messages, ["[test_deprecation] AnyClass is deprecated"])

    def test_class_renamed(self):
        class AnyClass(object):
            pass

        OldClass = deprecation.class_renamed("OldClass", AnyClass)

        OldClass()
        self.assertEqual(
            self.messages, ["[test_deprecation] OldClass is deprecated, use AnyClass instead"]
        )

    def test_class_renamed_conflict_metaclass(self):
        class SomeMetaClass(type):
            pass

        class AnyClass(metaclass=SomeMetaClass):
            pass

        # make sure the "metaclass conflict: the metaclass of a derived class # must be a
        # (non-strict) subclass of the metaclasses of all its bases" exception won't be raised
        deprecation.class_renamed("OldClass", AnyClass)

    def test_class_moved(self):
        class AnyClass(object):
            pass

        OldClass = deprecation.class_moved(new_class=AnyClass, old_name="OldName")
        OldClass()
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] class test_deprecation.OldName is now available as "
                "test_deprecation.AnyClass"
            ],
        )

        self.messages = []

        AnyClass = deprecation.class_moved(new_class=AnyClass)

        AnyClass()
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] class test_deprecation.AnyClass is now available as "
                "test_deprecation.AnyClass"
            ],
        )

    def test_deprecated_func(self):
        any_func = deprecation.callable_deprecated()(self.mk_func())
        any_func()
        any_func = deprecation.callable_deprecated("message")(self.mk_func())
        any_func()
        self.assertEqual(
            self.messages,
            [
                '[test_deprecation] The function "any_func" is deprecated',
                "[test_deprecation] message",
            ],
        )

    def test_deprecated_decorator(self):
        @deprecation.callable_deprecated()
        def any_func():
            pass

        any_func()

        @deprecation.callable_deprecated("message")
        def any_func():
            pass

        any_func()
        self.assertEqual(
            self.messages,
            [
                '[test_deprecation] The function "any_func" is deprecated',
                "[test_deprecation] message",
            ],
        )

    def test_deprecated_decorator_bad_lazyobject(self):
        # this should not raised an ImportationError
        deprecation.deprecated("foobar")(LazyObject("cubes.localperms", "xperm"))

        # with or without giving it a message (because it shouldn't access
        # attributes of the wrapped object before the object is called)
        deprecation.deprecated()(LazyObject("cubes.localperms", "xperm"))

        # all of this is done because of the magical way LazyObject is working
        # and that sometime CW used to use it to do fake import on deprecated
        # modules to raise a warning if they were used but not importing them
        # by default.
        # See: https://forge.extranet.logilab.fr/cubicweb/cubicweb/blob/3.24.0/cubicweb/schemas/__init__.py#L51 # noqa

    def test_lazy_wraps_function_name(self):
        """
        Avoid conflict from lazy_wraps where __name__ isn't correctly set on
        the wrapper from the wrapped and we end up with the name of the wrapper
        instead of the wrapped.

        Like here it would fail if "check_kwargs" is the name of the new
        function instead of new_function_name, this is because the wrapper in
        argument_renamed is called check_kwargs and doesn't transmit the
        __name__ of the wrapped (new_function_name) correctly.
        """

        @deprecation.argument_renamed(old_name="a", new_name="b")
        def new_function_name(b):
            pass

        old_function_name = deprecation.callable_renamed(
            old_name="old_function_name", new_function=new_function_name
        )
        old_function_name(None)

        assert "old_function_name" in self.messages[0]
        assert "new_function_name" in self.messages[0]
        assert "check_kwargs" not in self.messages[0]

    def test_attribute_renamed(self):
        @deprecation.attribute_renamed(old_name="old", new_name="new")
        class SomeClass:
            def __init__(self):
                self.new = 42

        some_class = SomeClass()
        self.assertEqual(some_class.old, some_class.new)
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] SomeClass.old has been renamed and is deprecated, "
                "use SomeClass.new instead"
            ],
        )

        some_class.old = 43
        self.assertEqual(some_class.old, 43)
        self.assertEqual(some_class.old, some_class.new)

        self.assertTrue(hasattr(some_class, "new"))
        self.assertTrue(hasattr(some_class, "old"))
        del some_class.old
        self.assertFalse(hasattr(some_class, "new"))
        self.assertFalse(hasattr(some_class, "old"))

    def test_argument_renamed(self):
        @deprecation.argument_renamed(old_name="old", new_name="new")
        def some_function(new):
            return new

        self.assertEqual(some_function(new=42), 42)
        self.assertEqual(some_function(old=42), 42)
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] argument old of callable some_function has been renamed and is "
                "deprecated, use keyword argument new instead"
            ],
        )

        with self.assertRaises(ValueError):
            some_function(new=42, old=42)

    def test_argument_removed(self):
        @deprecation.argument_removed("old")
        def some_function(new):
            return new

        self.assertEqual(some_function(new=42), 42)
        self.assertEqual(some_function(new=10, old=20), 10)
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] argument old of callable some_function has been removed and is "
                "deprecated"
            ],
        )

    def test_callable_renamed(self):
        def any_func():
            pass

        old_func = deprecation.callable_renamed("old_func", any_func)
        old_func()

        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] old_func has been renamed and is deprecated, "
                "uses any_func instead"
            ],
        )

    def test_callable_moved(self):
        module = "data.deprecation"
        moving_target = deprecation.callable_moved(module, "moving_target")
        moving_target()
        self.assertEqual(
            self.messages,
            [
                "[test_deprecation] object test_deprecation.moving_target has been moved to "
                "data.deprecation.moving_target"
            ],
        )


class StructuredDeprecatedWarningsTest(TestCase):
    def mock_warn(self, *args, **kwargs):
        self.collected_warnings.append(args[0])

    def setUp(self):
        self.collected_warnings = []
        deprecation.warn = self.mock_warn

    def tearDown(self):
        deprecation.warn = warnings.warn

    def mk_func(self):
        def any_func():
            pass

        return any_func

    def test_class_deprecated(self):
        class AnyClass(metaclass=deprecation.class_deprecated):
            pass

        AnyClass()
        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)

    def test_class_renamed(self):
        class AnyClass:
            pass

        OldClass = deprecation.class_renamed("OldClass", AnyClass)

        OldClass()
        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "OldClass")
        self.assertEqual(warning.new_name, "AnyClass")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)

    def test_class_moved(self):
        class AnyClass:
            pass

        OldClass = deprecation.class_moved(new_class=AnyClass, old_name="OldName")
        OldClass()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
        self.assertEqual(warning.old_module, "test_deprecation")
        self.assertEqual(warning.new_module, "test_deprecation")
        self.assertEqual(warning.old_name, "OldName")
        self.assertEqual(warning.new_name, "AnyClass")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)

        self.collected_warnings = []

        AnyClass = deprecation.class_moved(new_class=AnyClass)

        AnyClass()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
        self.assertEqual(warning.old_module, "test_deprecation")
        self.assertEqual(warning.new_module, "test_deprecation")
        self.assertEqual(warning.old_name, "AnyClass")
        self.assertEqual(warning.new_name, "AnyClass")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)

    def test_deprecated_func(self):
        any_func = deprecation.callable_deprecated()(self.mk_func())
        any_func()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)

        any_func = deprecation.callable_deprecated("message")(self.mk_func())
        any_func()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)

    def test_deprecated_decorator(self):
        @deprecation.callable_deprecated()
        def any_func():
            pass

        any_func()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)

        @deprecation.callable_deprecated("message")
        def any_func():
            pass

        any_func()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)

    def test_attribute_renamed(self):
        @deprecation.attribute_renamed(old_name="old", new_name="new")
        class SomeClass:
            def __init__(self):
                self.new = 42

        some_class = SomeClass()

        some_class.old == some_class.new  # trigger warning

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "old")
        self.assertEqual(warning.new_name, "new")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)

        some_class.old = 43

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "old")
        self.assertEqual(warning.new_name, "new")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)

        del some_class.old

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "old")
        self.assertEqual(warning.new_name, "new")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)

    def test_argument_renamed(self):
        @deprecation.argument_renamed(old_name="old", new_name="new")
        def some_function(new):
            return new

        some_function(old=42)

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "old")
        self.assertEqual(warning.new_name, "new")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ARGUMENT)

    def test_argument_removed(self):
        @deprecation.argument_removed("old")
        def some_function(new):
            return new

        some_function(new=10, old=20)

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.REMOVED)
        self.assertEqual(warning.name, "old")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ARGUMENT)

    def test_callable_renamed(self):
        def any_func():
            pass

        old_func = deprecation.callable_renamed("old_func", any_func)
        old_func()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
        self.assertEqual(warning.old_name, "old_func")
        self.assertEqual(warning.new_name, "any_func")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)

    def test_callable_moved(self):
        module = "data.deprecation"
        moving_target = deprecation.callable_moved(module, "moving_target")
        moving_target()

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
        self.assertEqual(warning.old_module, "test_deprecation")
        self.assertEqual(warning.new_module, "data.deprecation")
        self.assertEqual(warning.old_name, "moving_target")
        self.assertEqual(warning.new_name, "moving_target")
        self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)


class DeprecatedWarningsTracebackLocationTest(TestCase):
    def setUp(self):
        self.catch_warnings = warnings.catch_warnings(record=True)
        self.collected_warnings = self.catch_warnings.__enter__()

    def tearDown(self):
        self.catch_warnings.__exit__()

    def mk_func(self):
        def any_func():
            pass

        return any_func

    def test_class_deprecated(self):
        class AnyClass(metaclass=deprecation.class_deprecated):
            pass

        AnyClass()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_class_renamed(self):
        class AnyClass:
            pass

        OldClass = deprecation.class_renamed("OldClass", AnyClass)

        OldClass()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_class_moved(self):
        class AnyClass:
            pass

        OldClass = deprecation.class_moved(new_class=AnyClass, old_name="OldName")
        OldClass()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

        AnyClass = deprecation.class_moved(new_class=AnyClass)

        AnyClass()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_deprecated_func(self):
        any_func = deprecation.callable_deprecated()(self.mk_func())
        any_func()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

        any_func = deprecation.callable_deprecated("message")(self.mk_func())
        any_func()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_deprecated_decorator(self):
        @deprecation.callable_deprecated()
        def any_func():
            pass

        any_func()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

        @deprecation.callable_deprecated("message")
        def any_func():
            pass

        any_func()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_attribute_renamed(self):
        @deprecation.attribute_renamed(old_name="old", new_name="new")
        class SomeClass:
            def __init__(self):
                self.new = 42

        some_class = SomeClass()

        some_class.old == some_class.new  # trigger warning
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

        some_class.old = 43
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

        del some_class.old
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_argument_renamed(self):
        @deprecation.argument_renamed(old_name="old", new_name="new")
        def some_function(new):
            return new

        some_function(old=42)
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_argument_removed(self):
        @deprecation.argument_removed("old")
        def some_function(new):
            return new

        some_function(new=10, old=20)
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_callable_renamed(self):
        def any_func():
            pass

        old_func = deprecation.callable_renamed("old_func", any_func)
        old_func()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)

    def test_callable_moved(self):
        module = "data.deprecation"
        moving_target = deprecation.callable_moved(module, "moving_target")
        moving_target()
        warning_line = getframeinfo(currentframe()).lineno - 1

        self.assertEqual(len(self.collected_warnings), 1)
        warning = self.collected_warnings.pop()

        location = f"{CURRENT_FILE}:{warning_line}"
        self.assertEqual(f"{warning.filename}:{warning.lineno}", location)


if __name__ == "__main__":
    unittest_main()