|
16 | 16 | assert_no_warnings, |
17 | 17 | denorm_cosine_distance, |
18 | 18 | deprecated_argument, |
| 19 | + deprecated_class, |
19 | 20 | deprecated_function, |
20 | 21 | lazy_import, |
21 | 22 | norm_cosine_distance, |
@@ -534,6 +535,94 @@ def test_logging_configuration_not_overridden(self): |
534 | 535 | ), f"Date format changed: was present before: {has_date_pre}, present after: {has_date_post}" |
535 | 536 |
|
536 | 537 |
|
| 538 | +class TestDeprecatedClass: |
| 539 | + def test_deprecated_class_warning_with_replacement(self): |
| 540 | + @deprecated_class(replacement="Use NewClass instead.") |
| 541 | + class OldClass: |
| 542 | + def __init__(self, value): |
| 543 | + self.value = value |
| 544 | + |
| 545 | + with pytest.warns(DeprecationWarning) as record: |
| 546 | + obj = OldClass(42) |
| 547 | + |
| 548 | + assert len(record) == 1 |
| 549 | + assert str(record[0].message) == ( |
| 550 | + "Class OldClass is deprecated and will be removed in the next major release. " |
| 551 | + "Use NewClass instead." |
| 552 | + ) |
| 553 | + assert obj.value == 42 |
| 554 | + |
| 555 | + def test_deprecated_class_warning_without_replacement(self): |
| 556 | + @deprecated_class() |
| 557 | + class OldClass: |
| 558 | + def __init__(self, value): |
| 559 | + self.value = value |
| 560 | + |
| 561 | + with pytest.warns(DeprecationWarning) as record: |
| 562 | + obj = OldClass(42) |
| 563 | + |
| 564 | + assert len(record) == 1 |
| 565 | + assert str(record[0].message) == ( |
| 566 | + "Class OldClass is deprecated and will be removed in the next major release. " |
| 567 | + ) |
| 568 | + assert obj.value == 42 |
| 569 | + |
| 570 | + def test_deprecated_class_with_custom_name(self): |
| 571 | + @deprecated_class(name="CustomOldClass", replacement="Use NewClass instead.") |
| 572 | + class OldClass: |
| 573 | + pass |
| 574 | + |
| 575 | + with pytest.warns(DeprecationWarning) as record: |
| 576 | + OldClass() |
| 577 | + |
| 578 | + assert len(record) == 1 |
| 579 | + assert str(record[0].message) == ( |
| 580 | + "Class CustomOldClass is deprecated and will be removed in the next major release. " |
| 581 | + "Use NewClass instead." |
| 582 | + ) |
| 583 | + |
| 584 | + def test_deprecated_class_preserves_functionality(self): |
| 585 | + @deprecated_class(replacement="Use NewClass instead.") |
| 586 | + class OldClass: |
| 587 | + def __init__(self, x, y): |
| 588 | + self.x = x |
| 589 | + self.y = y |
| 590 | + |
| 591 | + def add(self): |
| 592 | + return self.x + self.y |
| 593 | + |
| 594 | + with pytest.warns(DeprecationWarning): |
| 595 | + obj = OldClass(10, 20) |
| 596 | + |
| 597 | + assert obj.x == 10 |
| 598 | + assert obj.y == 20 |
| 599 | + assert obj.add() == 30 |
| 600 | + |
| 601 | + def test_deprecated_class_with_inheritance(self): |
| 602 | + @deprecated_class(replacement="Use NewBase instead.") |
| 603 | + class OldBase: |
| 604 | + def __init__(self, value): |
| 605 | + self.value = value |
| 606 | + |
| 607 | + class Derived(OldBase): |
| 608 | + def __init__(self, value, extra): |
| 609 | + super().__init__(value) |
| 610 | + self.extra = extra |
| 611 | + |
| 612 | + # Creating an instance of the deprecated base class should warn |
| 613 | + with pytest.warns(DeprecationWarning): |
| 614 | + base_obj = OldBase(42) |
| 615 | + |
| 616 | + # Creating an instance of the derived class should also warn |
| 617 | + # because it calls the deprecated __init__ |
| 618 | + with pytest.warns(DeprecationWarning): |
| 619 | + derived_obj = Derived(42, "extra") |
| 620 | + |
| 621 | + assert base_obj.value == 42 |
| 622 | + assert derived_obj.value == 42 |
| 623 | + assert derived_obj.extra == "extra" |
| 624 | + |
| 625 | + |
537 | 626 | class TestLazyImport: |
538 | 627 | def test_import_standard_library(self): |
539 | 628 | """Test lazy importing of a standard library module""" |
|
0 commit comments