Skip to content

Commit e7e1a0f

Browse files
authored
Validate pytest-mypy-plugins input file schema (#127)
Create a `schema.json` to: * Validate the input provided by the users * Offer in-editor validation and auto-completion * Easily keep the documentation of it up-to-date Use said schema to meta-test all test files for conformance. Additionally: * Fix `mypy_config` type to `str | None` * Update `jinja2.defaults.VARIABLE_START_STRING` to the more-correct `_rendering_env.variable_start_string`. * Update `.gitignore` This fixes the real issue behind #124: The problem was not that `mypy_config` *MUST HAVE* `{{` when `parametrized` was set; It was passing a `list` (of `dict`s) - which that was not templatable. Signed-off-by: Stavros Ntentos <[email protected]>
1 parent cf14d31 commit e7e1a0f

File tree

7 files changed

+429
-10
lines changed

7 files changed

+429
-10
lines changed

.gitignore

+165-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,169 @@
11
.idea/
2-
*.egg-info
3-
.mypy_cache
4-
__pycache__
5-
dist/
2+
3+
## Mostly complete version from https://github.com/github/gitignore/blob/e5323759e387ba347a9d50f8b0ddd16502eb71d4/Python.gitignore
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
615
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
pip-wheel-metadata/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
*.py,cover
55+
.hypothesis/
756
.pytest_cache/
57+
cover/
58+
cache/*
59+
60+
# Translations
61+
*.mo
62+
*.pot
63+
64+
# Django stuff:
65+
*.log
66+
local_settings.py
67+
db.sqlite3
68+
db.sqlite3-journal
69+
70+
# Flask stuff:
71+
instance/
72+
.webassets-cache
73+
74+
# Scrapy stuff:
75+
.scrapy
76+
77+
# Sphinx documentation
78+
docs/_build/
79+
80+
# PyBuilder
81+
.pybuilder/
82+
target/
83+
84+
# Jupyter Notebook
85+
.ipynb_checkpoints
86+
87+
# IPython
88+
profile_default/
89+
ipython_config.py
90+
91+
# pyenv
92+
# For a library or package, you might want to ignore these files since the code is
93+
# intended to run in multiple environments; otherwise, check them in:
94+
# .python-version
95+
96+
# pipenv
97+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
98+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
99+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
100+
# install all needed dependencies.
101+
#Pipfile.lock
102+
103+
# poetry
104+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105+
# This is especially recommended for binary packages to ensure reproducibility, and is more
106+
# commonly ignored for libraries.
107+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108+
#poetry.lock
109+
110+
# pdm
111+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112+
#pdm.lock
113+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114+
# in version control.
115+
# https://pdm.fming.dev/#use-with-ide
116+
.pdm.toml
117+
118+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
119+
__pypackages__/
120+
121+
# Celery stuff
122+
celerybeat-schedule
123+
celerybeat.pid
124+
125+
# SageMath parsed files
126+
*.sage.py
127+
128+
# Environments
129+
.env
130+
.venv
131+
env/
8132
venv/
133+
ENV/
134+
env.bak/
135+
venv.bak/
136+
137+
# Spyder project settings
138+
.spyderproject
139+
.spyproject
140+
141+
# Rope project settings
142+
.ropeproject
143+
144+
# mkdocs documentation
145+
/site
146+
147+
# mypy
148+
.mypy_cache/
149+
.dmypy.json
150+
dmypy.json
151+
152+
# Pyre type checker
153+
.pyre/
154+
155+
# pytype static type analyzer
156+
.pytype/
157+
158+
# Cython debug symbols
159+
cython_debug/
160+
161+
# PyCharm
162+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
163+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164+
# and can be added to the global gitignore or merged into this file. For a more nuclear
165+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
166+
#.idea/
167+
168+
# VS code
169+
.vscode/launch.json

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ On top of that, each case must comply to following types:
6868
| `main` | `str` | Portion of the code as if written in `.py` file |
6969
| `files` | `Optional[List[File]]=[]`\* | List of extra files to simulate imports if needed |
7070
| `disable_cache` | `Optional[bool]=False` | Set to `true` disables `mypy` caching |
71-
| `mypy_config` | `Optional[Dict[str, Union[str, int, bool, float]]]={}` | Inline `mypy` configuration, passed directly to `mypy` as `--config-file` option, possibly joined with `--mypy-pyproject-toml-file` or `--mypy-ini-file` contents if they are passed. By default is treated as `ini`, treated as `toml` only if `--mypy-pyproject-toml-file` is passed |
71+
| `mypy_config` | `Optional[str] ` | Inline `mypy` configuration, passed directly to `mypy` as `--config-file` option, possibly joined with `--mypy-pyproject-toml-file` or `--mypy-ini-file` contents if they are passed. By default is treated as `ini`, treated as `toml` only if `--mypy-pyproject-toml-file` is passed |
7272
| `env` | `Optional[Dict[str, str]]={}` | Environmental variables to be provided inside of test run |
7373
| `parametrized` | `Optional[List[Parameter]]=[]`\* | List of parameters, similar to [`@pytest.mark.parametrize`](https://docs.pytest.org/en/stable/parametrize.html) |
7474
| `skip` | `str` | Expression evaluated with following globals set: `sys`, `os`, `pytest` and `platform` |
@@ -94,6 +94,14 @@ Implementation notes:
9494
[`eval`](https://docs.python.org/3/library/functions.html#eval). It is advised to take a peek and
9595
learn about how `eval` works.
9696

97+
Repository also offers a [JSONSchema](pytest_mypy_plugins/schema.json), with which
98+
it validates the input. It can also offer your editor auto-completions, descriptions, and validation.
99+
100+
All you have to do, add the following line at the top of your YAML file:
101+
```yaml
102+
# yaml-language-server: $schema=https://raw.githubusercontent.com/typeddjango/pytest-mypy-plugins/master/pytest_mypy_plugins/schema.json
103+
```
104+
97105
### Example
98106

99107
#### 1. Inline type expectations

pytest_mypy_plugins/collect.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import pathlib
34
import platform
@@ -16,6 +17,7 @@
1617
Set,
1718
)
1819

20+
import jsonschema
1921
import py.path
2022
import pytest
2123
import yaml
@@ -29,12 +31,29 @@
2931
from pytest_mypy_plugins.item import YamlTestItem
3032

3133

34+
SCHEMA = json.loads((pathlib.Path(__file__).parent / "schema.json").read_text("utf8"))
35+
SCHEMA["items"]["properties"]["__line__"] = {
36+
"type": "integer",
37+
"description": "Line number where the test starts (`pytest-mypy-plugins` internal)",
38+
}
39+
40+
3241
@dataclass
3342
class File:
3443
path: str
3544
content: str
3645

3746

47+
def validate_schema(data: Any) -> None:
48+
"""Validate the schema of the file-under-test."""
49+
# Unfortunately, yaml.safe_load() returns Any,
50+
# so we make our intention explicit here.
51+
if not isinstance(data, list):
52+
raise TypeError(f"Test file has to be YAML list, got {type(data)!r}.")
53+
54+
jsonschema.validate(instance=data, schema=SCHEMA)
55+
56+
3857
def parse_test_files(test_files: List[Dict[str, Any]]) -> List[File]:
3958
files: List[File] = []
4059
for test_file in test_files:
@@ -95,6 +114,8 @@ def collect(self) -> Iterator["YamlTestItem"]:
95114
if parsed_file is None:
96115
return
97116

117+
validate_schema(parsed_file)
118+
98119
if not isinstance(parsed_file, list):
99120
raise ValueError(f"Test file has to be YAML list, got {type(parsed_file)!r}.")
100121

0 commit comments

Comments
 (0)