|
31 | 31 | from pytest_mypy_plugins.item import YamlTestItem |
32 | 32 |
|
33 | 33 |
|
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 | | - |
41 | 34 | @dataclass |
42 | 35 | class File: |
43 | 36 | path: str |
44 | 37 | content: str |
45 | 38 |
|
46 | 39 |
|
47 | | -def validate_schema(data: Any) -> None: |
| 40 | +def validate_schema(data: Any, *, is_closed: bool = False) -> None: |
48 | 41 | """Validate the schema of the file-under-test.""" |
49 | 42 | # Unfortunately, yaml.safe_load() returns Any, |
50 | 43 | # so we make our intention explicit here. |
51 | 44 | if not isinstance(data, list): |
52 | 45 | raise TypeError(f"Test file has to be YAML list, got {type(data)!r}.") |
53 | 46 |
|
54 | | - jsonschema.validate(instance=data, schema=SCHEMA) |
| 47 | + schema = json.loads((pathlib.Path(__file__).parent / "schema.json").read_text("utf8")) |
| 48 | + schema["items"]["properties"]["__line__"] = { |
| 49 | + "type": "integer", |
| 50 | + "description": "Line number where the test starts (`pytest-mypy-plugins` internal)", |
| 51 | + } |
| 52 | + schema["items"]["additionalProperties"] = not is_closed |
| 53 | + |
| 54 | + jsonschema.validate(instance=data, schema=schema) |
55 | 55 |
|
56 | 56 |
|
57 | 57 | def parse_test_files(test_files: List[Dict[str, Any]]) -> List[File]: |
@@ -114,7 +114,7 @@ def collect(self) -> Iterator["YamlTestItem"]: |
114 | 114 | if parsed_file is None: |
115 | 115 | return |
116 | 116 |
|
117 | | - validate_schema(parsed_file) |
| 117 | + validate_schema(parsed_file, is_closed=self.config.option.mypy_closed_schema) |
118 | 118 |
|
119 | 119 | if not isinstance(parsed_file, list): |
120 | 120 | raise ValueError(f"Test file has to be YAML list, got {type(parsed_file)!r}.") |
@@ -220,3 +220,8 @@ def pytest_addoption(parser: Parser) -> None: |
220 | 220 | action="store_true", |
221 | 221 | help="mypy will ignore errors from site-packages", |
222 | 222 | ) |
| 223 | + group.addoption( |
| 224 | + "--mypy-closed-schema", |
| 225 | + action="store_true", |
| 226 | + help="Use closed schema to validate YAML test cases, which won't allow any extra keys (does not work well with `--mypy-extension-hook`)", |
| 227 | + ) |
0 commit comments