docs: Fix bug that caused the word "Beta" to appear twice in doc-strings (#17704)

The current issue:
Several beta descriptions in the API Reference are duplicated. For
example:
`[Beta] Get a context value.[Beta] Get a context value.` for the
[ContextGet
class](https://api.python.langchain.com/en/latest/core_api_reference.html#module-langchain_core.beta)
description.

NOTE: I've tested it only with a new ut! I cannot build API Reference
locally :(
This PR related to #17615
This commit is contained in:
Leonid Ganeline 2024-02-18 18:38:37 -08:00 committed by GitHub
parent 73edf17b4e
commit 1d2aa19aee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 80 additions and 15 deletions

View File

@ -206,10 +206,9 @@ def beta(
old_doc = inspect.cleandoc(old_doc or "").strip("\n")
# old_doc can be None
if not old_doc:
new_doc = "[*Beta*]"
else:
new_doc = f"[*Beta*] {old_doc}"
old_doc = ""
# Modify the docstring to include a beta notice.
notes_header = "\nNotes\n-----"
@ -218,7 +217,7 @@ def beta(
addendum,
]
details = " ".join([component.strip() for component in components if component])
new_doc += (
new_doc = (
f"[*Beta*] {old_doc}\n"
f"{notes_header if notes_header not in old_doc else ''}\n"
f".. beta::\n"

View File

@ -112,7 +112,7 @@ def test_beta_function() -> None:
doc = beta_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
assert not inspect.iscoroutinefunction(beta_function)
@ -132,7 +132,7 @@ async def test_beta_async_function() -> None:
doc = beta_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
assert inspect.iscoroutinefunction(beta_async_function)
@ -152,7 +152,7 @@ def test_beta_method() -> None:
doc = obj.beta_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
assert not inspect.iscoroutinefunction(obj.beta_method)
@ -173,7 +173,7 @@ async def test_beta_async_method() -> None:
doc = obj.beta_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
assert inspect.iscoroutinefunction(obj.beta_async_method)
@ -192,7 +192,7 @@ def test_beta_classmethod() -> None:
doc = ClassWithBetaMethods.beta_classmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
def test_beta_staticmethod() -> None:
@ -211,7 +211,7 @@ def test_beta_staticmethod() -> None:
)
doc = ClassWithBetaMethods.beta_staticmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
def test_beta_property() -> None:
@ -231,13 +231,12 @@ def test_beta_property() -> None:
)
doc = ClassWithBetaMethods.beta_property.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")
def test_whole_class_deprecation() -> None:
"""Test whole class deprecation."""
def test_whole_class_beta() -> None:
"""Test whole class beta status."""
# Test whole class deprecation
@beta()
class BetaClass:
def __init__(self) -> None:
@ -269,6 +268,73 @@ def test_whole_class_deprecation() -> None:
)
def test_whole_class_inherited_beta() -> None:
"""Test whole class beta status for inherited class.
The original version of beta decorator created duplicates with
'[*Beta*]'.
"""
# Test whole class beta status
@beta()
class BetaClass:
@beta()
def beta_method(self) -> str:
"""original doc"""
return "This is a beta method."
@beta()
class InheritedBetaClass(BetaClass):
@beta()
def beta_method(self) -> str:
"""original doc"""
return "This is a beta method 2."
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = BetaClass()
assert obj.beta_method() == "This is a beta method."
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 "
"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 API may change."
)
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = InheritedBetaClass()
assert obj.beta_method() == "This is a beta method 2."
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `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. "
"It is actively being worked on, so "
"the API may change."
)
# if [*Beta*] was inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Beta*]") == 1
# Tests with pydantic models
class MyModel(BaseModel):
@beta()
@ -292,4 +358,4 @@ def test_beta_method_pydantic() -> None:
doc = obj.beta_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Beta*] original doc")
assert doc.startswith("[*Beta*] original doc")