|
| 1 | +"""Tests for deprecation warnings on deprecated functions.""" |
| 2 | + |
| 3 | +import warnings |
| 4 | +import pytest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from langfuse import Langfuse |
| 8 | + |
| 9 | + |
| 10 | +class TestDeprecationWarnings: |
| 11 | + """Test that deprecated functions emit proper deprecation warnings.""" |
| 12 | + |
| 13 | + # List of deprecated functions and their expected warning messages. Target is the object they are called on. |
| 14 | + DEPRECATED_FUNCTIONS = [ |
| 15 | + # on the client: |
| 16 | + { |
| 17 | + "method": "start_generation", |
| 18 | + "target": "client", |
| 19 | + "kwargs": {"name": "test_generation"}, |
| 20 | + "expected_message": "start_generation is deprecated and will be removed in a future version. Use start_observation(as_type='generation') instead.", |
| 21 | + }, |
| 22 | + { |
| 23 | + "method": "start_as_current_generation", |
| 24 | + "target": "client", |
| 25 | + "kwargs": {"name": "test_generation"}, |
| 26 | + "expected_message": "start_as_current_generation is deprecated and will be removed in a future version. Use start_as_current_observation(as_type='generation') instead.", |
| 27 | + }, |
| 28 | + # on the span: |
| 29 | + { |
| 30 | + "method": "start_generation", |
| 31 | + "target": "span", |
| 32 | + "kwargs": {"name": "test_generation"}, |
| 33 | + "expected_message": "start_generation is deprecated and will be removed in a future version. Use start_observation(as_type='generation') instead.", |
| 34 | + }, |
| 35 | + { |
| 36 | + "method": "start_as_current_generation", |
| 37 | + "target": "span", |
| 38 | + "kwargs": {"name": "test_generation"}, |
| 39 | + "expected_message": "start_as_current_generation is deprecated and will be removed in a future version. Use start_as_current_observation(as_type='generation') instead.", |
| 40 | + }, |
| 41 | + { |
| 42 | + "method": "start_as_current_span", |
| 43 | + "target": "span", |
| 44 | + "kwargs": {"name": "test_span"}, |
| 45 | + "expected_message": "start_as_current_span is deprecated and will be removed in a future version. Use start_as_current_observation(as_type='span') instead.", |
| 46 | + }, |
| 47 | + ] |
| 48 | + |
| 49 | + @pytest.fixture |
| 50 | + def langfuse_client(self): |
| 51 | + """Create a Langfuse client for testing.""" |
| 52 | + with patch.dict( |
| 53 | + "os.environ", |
| 54 | + { |
| 55 | + "LANGFUSE_PUBLIC_KEY": "test_key", |
| 56 | + "LANGFUSE_SECRET_KEY": "test_secret", |
| 57 | + "LANGFUSE_HOST": "http://localhost:3000", |
| 58 | + }, |
| 59 | + ): |
| 60 | + return Langfuse() |
| 61 | + |
| 62 | + @pytest.mark.parametrize("func_info", DEPRECATED_FUNCTIONS) |
| 63 | + def test_deprecated_function_warnings(self, langfuse_client, func_info): |
| 64 | + """Test that deprecated functions emit proper deprecation warnings.""" |
| 65 | + method_name = func_info["method"] |
| 66 | + target = func_info["target"] |
| 67 | + kwargs = func_info["kwargs"] |
| 68 | + expected_message = func_info["expected_message"] |
| 69 | + |
| 70 | + with warnings.catch_warnings(record=True) as warning_list: |
| 71 | + warnings.simplefilter("always") |
| 72 | + |
| 73 | + try: |
| 74 | + if target == "client": |
| 75 | + # Test deprecated methods on the client |
| 76 | + method = getattr(langfuse_client, method_name) |
| 77 | + if "current" in method_name: |
| 78 | + # Context manager methods |
| 79 | + with method(**kwargs) as obj: |
| 80 | + if hasattr(obj, "end"): |
| 81 | + obj.end() |
| 82 | + else: |
| 83 | + # Regular methods |
| 84 | + obj = method(**kwargs) |
| 85 | + if hasattr(obj, "end"): |
| 86 | + obj.end() |
| 87 | + |
| 88 | + elif target == "span": |
| 89 | + # Test deprecated methods on spans |
| 90 | + span = langfuse_client.start_span(name="test_parent") |
| 91 | + method = getattr(span, method_name) |
| 92 | + if "current" in method_name: |
| 93 | + # Context manager methods |
| 94 | + with method(**kwargs) as obj: |
| 95 | + if hasattr(obj, "end"): |
| 96 | + obj.end() |
| 97 | + else: |
| 98 | + # Regular methods |
| 99 | + obj = method(**kwargs) |
| 100 | + if hasattr(obj, "end"): |
| 101 | + obj.end() |
| 102 | + span.end() |
| 103 | + |
| 104 | + except Exception: |
| 105 | + pass |
| 106 | + |
| 107 | + # Check that a deprecation warning was emitted |
| 108 | + deprecation_warnings = [ |
| 109 | + w for w in warning_list if issubclass(w.category, DeprecationWarning) |
| 110 | + ] |
| 111 | + assert ( |
| 112 | + len(deprecation_warnings) > 0 |
| 113 | + ), f"No DeprecationWarning emitted for {target}.{method_name}" |
| 114 | + |
| 115 | + # Check that the warning message matches expected |
| 116 | + warning_messages = [str(w.message) for w in deprecation_warnings] |
| 117 | + assert ( |
| 118 | + expected_message in warning_messages |
| 119 | + ), f"Expected warning message not found for {target}.{method_name}. Got: {warning_messages}" |
0 commit comments