Skip to content

Commit f001c33

Browse files
authored
Minor fixes (#1)
* fixed logo in readme * fixed type validation
1 parent a620a61 commit f001c33

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<picture>
22
<source media="(prefers-color-scheme: dark)" srcset="docs/images/logo.dark.png" width="300">
33
<source media="(prefers-color-scheme: light)" srcset="docs/images/logo.light.png" width="300">
4-
<img alt="logo" src="docs/images/logo.light.png" width="300">
4+
<img alt="logo" src="https://github.com/n-splv/promptsub-py/raw/main/docs/images/logo.light.png" width="300">
55
</picture>
66

77
# Prompt substitution for humans

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "promptsub"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "Creating parametrized prompts for language models"
55
authors = ["n-splv <[email protected]>"]
66
license = "MIT"

src/promptsub/prompt_api.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
from functools import cached_property
23

34
from attrs import (
@@ -16,10 +17,18 @@
1617
)
1718

1819

19-
template_factory = Factory(
20-
lambda self: Template(input_text=self.template_text),
21-
takes_self=True
22-
)
20+
def _init_template(self: Prompt) -> Template:
21+
"""
22+
Manual type check because `attrs.validators` is a bit broken:
23+
https://github.com/python-attrs/attrs/issues/1237
24+
25+
Can be replaced with a simple lambda if it gets fixed.
26+
"""
27+
if not isinstance(self.template_text, str):
28+
err_message = "Template text must be a string"
29+
raise TypeError(err_message)
30+
31+
return Template(input_text=self.template_text)
2332

2433

2534
@frozen
@@ -43,7 +52,9 @@ class Prompt:
4352
template_text: str = field(validator=validators.instance_of(str))
4453

4554
_template: Template = field(
46-
init=False, repr=False, default=template_factory
55+
init=False,
56+
repr=False,
57+
default=Factory(_init_template, takes_self=True)
4758
)
4859

4960
def substitute(self,

tests/test_errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from promptsub import special_chatacters as sc
88

99

10-
def raises(exception):
10+
def raises(exception, match: str = None):
1111
def decorator(func):
1212
@wraps(func)
1313
def wrapper(*args, **kwargs):
14-
with pytest.raises(exception):
14+
with pytest.raises(exception, match=match):
1515
func(*args, **kwargs)
1616
return wrapper
1717
return decorator
@@ -75,7 +75,7 @@ def test_component_not_closed(self, template):
7575
def test_empty_template(self, template):
7676
Prompt(template)
7777

78-
@raises(TypeError)
78+
@raises(TypeError, match="Template text must be a string")
7979
@pytest.mark.parametrize("template", (
8080
None,
8181
type("Foo", (), {})(),

0 commit comments

Comments
 (0)