From 30c79515052056c452294aa45193bed99e34d6f5 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Tue, 23 Apr 2024 11:20:13 -0700 Subject: [PATCH] core: use qualname in beta message (#20361) --- .../langchain_core/_api/beta_decorator.py | 11 +++-- .../unit_tests/_api/test_beta_decorator.py | 44 ++++++++++++------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/libs/core/langchain_core/_api/beta_decorator.py b/libs/core/langchain_core/_api/beta_decorator.py index 280c1c99c27..a481f10338f 100644 --- a/libs/core/langchain_core/_api/beta_decorator.py +++ b/libs/core/langchain_core/_api/beta_decorator.py @@ -121,7 +121,7 @@ def beta( if not _obj_type: _obj_type = "class" wrapped = obj.__init__ # type: ignore - _name = _name or obj.__name__ + _name = _name or obj.__qualname__ old_doc = obj.__doc__ def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: @@ -147,10 +147,11 @@ def beta( return cast(T, obj) elif isinstance(obj, property): + # note(erick): this block doesn't seem to be used? if not _obj_type: _obj_type = "attribute" wrapped = None - _name = _name or obj.fget.__name__ + _name = _name or obj.fget.__qualname__ old_doc = obj.__doc__ class _beta_property(property): @@ -189,10 +190,12 @@ def beta( ) else: + _name = _name or obj.__qualname__ if not _obj_type: - _obj_type = "function" + # edge case: when a function is within another function + # within a test, this will call it a "method" not a "function" + _obj_type = "function" if "." not in _name else "method" wrapped = obj - _name = _name or obj.__name__ old_doc = wrapped.__doc__ def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: diff --git a/libs/core/tests/unit_tests/_api/test_beta_decorator.py b/libs/core/tests/unit_tests/_api/test_beta_decorator.py index caef30c251c..634773344ce 100644 --- a/libs/core/tests/unit_tests/_api/test_beta_decorator.py +++ b/libs/core/tests/unit_tests/_api/test_beta_decorator.py @@ -35,7 +35,8 @@ from langchain_core.pydantic_v1 import BaseModel "obj_type": "", "addendum": "Please migrate your code.", }, - "`SomeFunction` is in beta. It is actively being worked on, so the API may " + "`SomeFunction` is in beta. It is actively being worked on, " + "so the API may " "change. Please migrate your code.", ), ], @@ -106,7 +107,8 @@ def test_beta_function() -> None: assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning) == ( - "The function `beta_function` is in beta. It is actively being worked on, " + "The function `beta_function` is in beta. It is actively being " + "worked on, " "so the API may change." ) @@ -146,7 +148,8 @@ def test_beta_method() -> None: assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning) == ( - "The function `beta_method` is in beta. It is actively being worked on, so " + "The method `ClassWithBetaMethods.beta_method` is in beta. It is actively " + "being worked on, so " "the API may change." ) @@ -167,7 +170,7 @@ async def test_beta_async_method() -> None: assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning) == ( - "The function `beta_async_method` is in beta. " + "The method `ClassWithBetaMethods.beta_async_method` is in beta. " "It is actively being worked on, so the API may change." ) @@ -186,8 +189,8 @@ def test_beta_classmethod() -> None: assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning) == ( - "The function `beta_classmethod` is in beta. It is actively being worked " - "on, so the API may change." + "The method `ClassWithBetaMethods.beta_classmethod` is in beta. " + "It is actively being worked on, so the API may change." ) doc = ClassWithBetaMethods.beta_classmethod.__doc__ @@ -206,8 +209,8 @@ def test_beta_staticmethod() -> None: warning = warning_list[0].message assert str(warning) == ( - "The function `beta_staticmethod` is in beta. It is actively being worked " - "on, so the API may change." + "The method `ClassWithBetaMethods.beta_staticmethod` is in beta. " + "It is actively being worked on, so the API may change." ) doc = ClassWithBetaMethods.beta_staticmethod.__doc__ assert isinstance(doc, str) @@ -226,8 +229,8 @@ def test_beta_property() -> None: warning = warning_list[0].message assert str(warning) == ( - "The function `beta_property` is in beta. It is actively being worked on, " - "so the API may change." + "The method `ClassWithBetaMethods.beta_property` is in beta. " + "It is actively being worked on, so the API may change." ) doc = ClassWithBetaMethods.beta_property.__doc__ assert isinstance(doc, str) @@ -257,13 +260,15 @@ def test_whole_class_beta() -> None: assert len(warning_list) == 2 warning = warning_list[0].message assert str(warning) == ( - "The class `BetaClass` is in beta. It is actively being worked on, so the " + "The class `test_whole_class_beta..BetaClass` is in beta. " + "It is actively being worked on, so the " "API may change." ) warning = warning_list[1].message assert str(warning) == ( - "The function `beta_method` is in beta. It is actively being worked on, so " + "The method `test_whole_class_beta..BetaClass.beta_method` " + "is in beta. It is actively being worked on, so " "the API may change." ) @@ -299,13 +304,15 @@ def test_whole_class_inherited_beta() -> None: assert len(warning_list) == 2 warning = warning_list[0].message assert str(warning) == ( - "The class `BetaClass` is in beta. It is actively being worked on, so the " + "The class `test_whole_class_inherited_beta..BetaClass` " + "is in beta. It is actively being worked on, so the " "API may change." ) warning = warning_list[1].message assert str(warning) == ( - "The function `beta_method` is in beta. It is actively being worked on, so " + "The method `test_whole_class_inherited_beta..BetaClass." + "beta_method` is in beta. It is actively being worked on, so " "the API may change." ) @@ -318,14 +325,16 @@ def test_whole_class_inherited_beta() -> None: assert len(warning_list) == 2 warning = warning_list[0].message assert str(warning) == ( - "The class `InheritedBetaClass` is in beta. " + "The class `test_whole_class_inherited_beta..InheritedBetaClass` " + "is in beta. " "It is actively being worked on, so the " "API may change." ) warning = warning_list[1].message assert str(warning) == ( - "The function `beta_method` is in beta. " + "The method `test_whole_class_inherited_beta..InheritedBetaClass." + "beta_method` is in beta. " "It is actively being worked on, so " "the API may change." ) @@ -352,7 +361,8 @@ def test_beta_method_pydantic() -> None: assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning) == ( - "The function `beta_method` is in beta. It is actively being worked on, so " + "The method `MyModel.beta_method` is in beta. It is actively being " + "worked on, so " "the API may change." )