Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docstring linter and docstrings #2

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Main entrypoint into package."""
3 changes: 3 additions & 0 deletions langchain/formatting.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""Utilities for formatting strings."""
from string import Formatter


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

def check_unused_args(self, used_args, args, kwargs):
"""Check to see if extra parameters are passed."""
extra = set(kwargs).difference(used_args)
if extra:
raise KeyError(extra)

def vformat(self, format_string, args, kwargs):
"""Check that no arguments are provided."""
if len(args) > 0:
raise ValueError(
"No arguments should be provided, "
Expand Down
3 changes: 3 additions & 0 deletions langchain/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ class Prompt(BaseModel):
template: str

class Config:
"""Configuration for this pydantic object."""

extra = Extra.forbid

@root_validator()
def template_is_valid(cls, values: Dict) -> Dict:
"""Check that template and input variables are consistent."""
input_variables = values["input_variables"]
template = values["template"]
dummy_inputs = {input_variable: "foo" for input_variable in input_variables}
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ black
isort
mypy
flake8
flake8-docstrings
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Set up the package."""
from setuptools import find_packages, setup

with open("README.md", "r") as f:
Expand Down
4 changes: 4 additions & 0 deletions tests/unit_tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
"""Test formatting functionality."""
import pytest

from langchain.formatting import formatter


def test_valid_formatting():
"""Test formatting works as expected."""
template = "This is a {foo} test."
output = formatter.format(template, foo="good")
expected_output = "This is a good test."
assert output == expected_output


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


def test_does_not_allow_extra_kwargs():
"""Test formatting does not allow extra key word arguments."""
template = "This is a {foo} test."
with pytest.raises(KeyError):
formatter.format(template, foo="good", bar="oops")
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test functionality related to prompts."""
import pytest

from langchain.prompt import Prompt


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


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


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


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