Skip to content

Commit e1e2f68

Browse files
Update version to 5.4.1 and add docstring validation for decorated functions
- Bump version to 5.4.1 to reflect recent changes - Implement validation to raise TypeError for functions without docstrings or with f-string docstrings - Add tests to ensure proper error handling for docstring validation
1 parent 2ad16e8 commit e1e2f68

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

promptic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from litellm import completion as litellm_completion
1818
from pydantic import BaseModel
1919

20-
__version__ = "5.4.0"
20+
__version__ = "5.4.1"
2121

2222
SystemPrompt = Optional[Union[str, List[str], List[Dict[str, str]]]]
2323

@@ -446,7 +446,13 @@ def wrapper(*args, **kwargs):
446446
)
447447

448448
# Get the function's docstring as the prompt
449-
prompt_template = dedent(func.__doc__)
449+
try:
450+
prompt_template = dedent(func.__doc__)
451+
except TypeError as e:
452+
raise TypeError(
453+
f"Function {func.__name__} has no docstring. "
454+
"Ensure the docstring is not an f-string."
455+
) from e
450456

451457
# Get the argument names, default values and values using inspect
452458
sig = inspect.signature(func)

tests/test_promptic.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,3 +1711,39 @@ def update_user(profile: UserProfile) -> str:
17111711
)
17121712

17131713
assert name == "John Doe"
1714+
1715+
1716+
@pytest.mark.parametrize("model", CHEAP_MODELS[:1])
1717+
@pytest.mark.parametrize(
1718+
"create_completion_fn", [openai_completion_fn, litellm_completion][:1]
1719+
)
1720+
def test_docstring_validation(model, create_completion_fn):
1721+
"""Test validation of docstrings in decorated functions"""
1722+
if create_completion_fn == openai_completion_fn and not model.startswith("gpt"):
1723+
pytest.skip("Non-GPT models are not supported with OpenAI client")
1724+
1725+
# Test function with no docstring
1726+
with pytest.raises(TypeError) as exc_info:
1727+
1728+
@llm(model=model, create_completion_fn=create_completion_fn)
1729+
def no_docstring_function(text):
1730+
pass
1731+
1732+
no_docstring_function("Hello")
1733+
1734+
assert "no_docstring_function has no docstring" in str(exc_info.value)
1735+
assert "Ensure the docstring is not an f-string" in str(exc_info.value)
1736+
1737+
# Test function with an f-string (this won't actually be executed but will fail at definition time)
1738+
name = "World"
1739+
with pytest.raises(TypeError) as exc_info:
1740+
# This would fail because f-strings aren't allowed in docstrings
1741+
@llm(model=model, create_completion_fn=create_completion_fn)
1742+
def f_string_docstring(text):
1743+
f"""Hello {name}"""
1744+
return text
1745+
1746+
f_string_docstring("Hello")
1747+
1748+
assert "f_string_docstring has no docstring" in str(exc_info.value)
1749+
assert "Ensure the docstring is not an f-string" in str(exc_info.value)

0 commit comments

Comments
 (0)