Skip to content

Commit 24e69ea

Browse files
authored
Replace Pystache with Chevron. (#57)
* Relace Pystache with Chevron. * Don't pin chevron version. * Add unit test for render_template().
1 parent becaf8a commit 24e69ea

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

pytest_mypy_plugins/collect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import tempfile
55
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional
66

7-
import pystache
87
import pytest
98
import yaml
109
from _pytest.config.argparsing import Parser
@@ -99,7 +98,8 @@ def collect(self) -> Iterator["YamlTestItem"]:
9998
test_name_suffix = ""
10099

101100
test_name = f"{test_name_prefix}{test_name_suffix}"
102-
main_file = File(path="main.py", content=pystache.render(raw_test["main"], params))
101+
main_content = utils.render_template(template=raw_test["main"], data=params)
102+
main_file = File(path="main.py", content=main_content)
103103
test_files = [main_file] + parse_test_files(raw_test.get("files", []))
104104
expect_fail = raw_test.get("expect_fail", False)
105105
regex = raw_test.get("regex", False)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# encoding=utf-8
2+
from pytest_mypy_plugins import utils
3+
4+
5+
def test_render_template_with_None_value() -> None:
6+
# Given
7+
template = "{{ a }} {{ b }}"
8+
data = {"a": None, "b": 99}
9+
10+
# When
11+
actual = utils.render_template(template=template, data=data)
12+
13+
# Then
14+
assert actual == "None 99"

pytest_mypy_plugins/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Union,
2020
)
2121

22-
import pystache
22+
import chevron
2323
import regex
2424
from decorator import contextmanager
2525

@@ -341,7 +341,8 @@ def extract_output_matchers_from_out(out: str, params: Mapping[str, Any], regex:
341341
The result is a list of output matchers
342342
"""
343343
matchers = []
344-
for line in pystache.render(out, params).split("\n"):
344+
lines = render_template(out, params).split("\n")
345+
for line in lines:
345346
match = re.search(
346347
r"^(?P<fname>.*):(?P<lnum>\d*): (?P<severity>.*):((?P<col>\d+):)? (?P<message>.*)$", line.strip()
347348
)
@@ -368,6 +369,10 @@ def extract_output_matchers_from_out(out: str, params: Mapping[str, Any], regex:
368369
return matchers
369370

370371

372+
def render_template(template: str, data: Mapping[str, Any]) -> str:
373+
return chevron.render(template=template, data={k: v if v is not None else "None" for k, v in data.items()})
374+
375+
371376
def get_func_first_lnum(attr: Callable[..., None]) -> Optional[Tuple[int, List[str]]]:
372377
lines, _ = inspect.getsourcelines(attr)
373378
for lnum, line in enumerate(lines):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"mypy>=0.790",
99
"decorator",
1010
"pyyaml",
11-
"pystache>=0.5.4",
11+
"chevron",
1212
]
1313

1414
setup(

0 commit comments

Comments
 (0)