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

enforce mypy and fix errors #3

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
12 changes: 10 additions & 2 deletions langchain/formatting.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
"""Utilities for formatting strings."""
from string import Formatter
from typing import Any, Mapping, Sequence, Union


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

def check_unused_args(self, used_args, args, kwargs):
def check_unused_args(
self,
used_args: Sequence[Union[int, str]],
args: Sequence,
kwargs: Mapping[str, Any],
) -> None:
"""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):
def vformat(
self, format_string: str, args: Sequence, kwargs: Mapping[str, Any]
) -> str:
"""Check that no arguments are provided."""
if len(args) > 0:
raise ValueError(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ profile = "black"

[tool.mypy]
ignore_missing_imports = "True"
disallow_untyped_defs = "True"
exclude = ["notebooks"]
6 changes: 3 additions & 3 deletions tests/unit_tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
from langchain.formatting import formatter


def test_valid_formatting():
def test_valid_formatting() -> None:
"""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():
def test_does_not_allow_args() -> None:
"""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():
def test_does_not_allow_extra_kwargs() -> None:
"""Test formatting does not allow extra key word arguments."""
template = "This is a {foo} test."
with pytest.raises(KeyError):
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from langchain.prompt import Prompt


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


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


def test_prompt_extra_input_variables():
def test_prompt_extra_input_variables() -> None:
"""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():
def test_prompt_wrong_input_variables() -> None:
"""Test error is raised when name of input variable is wrong."""
template = "This is a {foo} test."
input_variables = ["bar"]
Expand Down