Skip to content

Add message templates. #58

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ Implementation notes:
[`eval`](https://docs.python.org/3/library/functions.html#eval). It is advised to take a peek and
learn about how `eval` works.

### Example
### Placeholders

In addition to test cases, re-usable placeholders can be defined in a top level element names `placeholders`, pieces of
text which can be reused in any test cases within the YAML test file - see example below.

### Examples

#### 1. Inline type expectations

Expand Down Expand Up @@ -148,6 +153,22 @@ Implementation notes:
reveal_type(a) # NR: .*str.*
```

#### 6. Placeholders

```yaml
- case: test_placeholder
main: |
a = 1
b = 'hello'

reveal_type(a) # N: {{ reveal }} "builtins.int"
reveal_type(b) # NR: {{ reveal }} {{ str }}

- placeholders:
reveal: "Revealed type is"
str: ".*str.*"
```

## Options

```
Expand Down
8 changes: 6 additions & 2 deletions pytest_mypy_plugins/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
import sys
import tempfile
from collections import ChainMap
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional

import pytest
Expand Down Expand Up @@ -83,7 +84,10 @@ def collect(self) -> Iterator["YamlTestItem"]:
if not isinstance(parsed_file, list):
raise ValueError(f"Test file has to be YAML list, got {type(parsed_file)!r}.")

for raw_test in parsed_file:
templates = ChainMap(*[t["placeholders"] for t in parsed_file if "placeholders" in t])

raw_tests = [c for c in parsed_file if "case" in c]
for raw_test in raw_tests:
test_name_prefix = raw_test["case"]
if " " in test_name_prefix:
raise ValueError(f"Invalid test name {test_name_prefix!r}, only '[a-zA-Z0-9_]' is allowed.")
Expand All @@ -98,7 +102,7 @@ def collect(self) -> Iterator["YamlTestItem"]:
test_name_suffix = ""

test_name = f"{test_name_prefix}{test_name_suffix}"
main_content = utils.render_template(template=raw_test["main"], data=params)
main_content = utils.render_template(template=raw_test["main"], data=ChainMap(params, templates))
main_file = File(path="main.py", content=main_content)
test_files = [main_file] + parse_test_files(raw_test.get("files", []))
expect_fail = raw_test.get("expect_fail", False)
Expand Down
19 changes: 19 additions & 0 deletions pytest_mypy_plugins/tests/test-placeholders.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- case: test_placeholder
main: |
a = 1
b = 'hello'

reveal_type(a) # N: {{ reveal }} "builtins.int"
reveal_type(b) # NR: {{ reveal }} {{ str }}

- case: test_using_parametrized_for_templates
parametrized:
- rtype: "Revealed type is"
main: |
a = 1

reveal_type(a) # N: {{ rtype }} "builtins.int"

- placeholders:
reveal: "Revealed type is"
str: ".*str.*"