Skip to content

Commit c48e6aa

Browse files
authored
Merge pull request #2 from hwchase17/harrison/add_docstrings
add docstring linter and docstrings
2 parents b6c215e + 3b22c25 commit c48e6aa

File tree

7 files changed

+18
-0
lines changed

7 files changed

+18
-0
lines changed

langchain/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Main entrypoint into package."""

langchain/formatting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
"""Utilities for formatting strings."""
12
from string import Formatter
23

34

45
class StrictFormatter(Formatter):
56
"""A subclass of formatter that checks for extra keys."""
67

78
def check_unused_args(self, used_args, args, kwargs):
9+
"""Check to see if extra parameters are passed."""
810
extra = set(kwargs).difference(used_args)
911
if extra:
1012
raise KeyError(extra)
1113

1214
def vformat(self, format_string, args, kwargs):
15+
"""Check that no arguments are provided."""
1316
if len(args) > 0:
1417
raise ValueError(
1518
"No arguments should be provided, "

langchain/prompt.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ class Prompt(BaseModel):
1313
template: str
1414

1515
class Config:
16+
"""Configuration for this pydantic object."""
17+
1618
extra = Extra.forbid
1719

1820
@root_validator()
1921
def template_is_valid(cls, values: Dict) -> Dict:
22+
"""Check that template and input variables are consistent."""
2023
input_variables = values["input_variables"]
2124
template = values["template"]
2225
dummy_inputs = {input_variable: "foo" for input_variable in input_variables}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ black
44
isort
55
mypy
66
flake8
7+
flake8-docstrings

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Set up the package."""
12
from setuptools import find_packages, setup
23

34
with open("README.md", "r") as f:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
"""Test formatting functionality."""
12
import pytest
23

34
from langchain.formatting import formatter
45

56

67
def test_valid_formatting():
8+
"""Test formatting works as expected."""
79
template = "This is a {foo} test."
810
output = formatter.format(template, foo="good")
911
expected_output = "This is a good test."
1012
assert output == expected_output
1113

1214

1315
def test_does_not_allow_args():
16+
"""Test formatting raises error when args are provided."""
1417
template = "This is a {} test."
1518
with pytest.raises(ValueError):
1619
formatter.format(template, "good")
1720

1821

1922
def test_does_not_allow_extra_kwargs():
23+
"""Test formatting does not allow extra key word arguments."""
2024
template = "This is a {foo} test."
2125
with pytest.raises(KeyError):
2226
formatter.format(template, foo="good", bar="oops")

tests/unit_tests/test_schema.py renamed to tests/unit_tests/test_prompt.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
"""Test functionality related to prompts."""
12
import pytest
23

34
from langchain.prompt import Prompt
45

56

67
def test_prompt_valid():
8+
"""Test prompts can be constructed."""
79
template = "This is a {foo} test."
810
input_variables = ["foo"]
911
prompt = Prompt(input_variables=input_variables, template=template)
@@ -12,20 +14,23 @@ def test_prompt_valid():
1214

1315

1416
def test_prompt_missing_input_variables():
17+
"""Test error is raised when input variables are not provided."""
1518
template = "This is a {foo} test."
1619
input_variables = []
1720
with pytest.raises(ValueError):
1821
Prompt(input_variables=input_variables, template=template)
1922

2023

2124
def test_prompt_extra_input_variables():
25+
"""Test error is raised when there are too many input variables."""
2226
template = "This is a {foo} test."
2327
input_variables = ["foo", "bar"]
2428
with pytest.raises(ValueError):
2529
Prompt(input_variables=input_variables, template=template)
2630

2731

2832
def test_prompt_wrong_input_variables():
33+
"""Test error is raised when name of input variable is wrong."""
2934
template = "This is a {foo} test."
3035
input_variables = ["bar"]
3136
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)