Skip to content

Commit

Permalink
Version 3.1.1 release (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Mar 13, 2024
1 parent 2b79a71 commit 63ed231
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Expand Up @@ -19,9 +19,9 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
pytest-version: ["~=7.2", "~=8.0"]
pytest-version: ["~=7.2", "~=8.1"]
# TODO: remove after several new versions of mypy
mypy-version: ["~=1.7", "~=1.8"]
mypy-version: ["~=1.7", "~=1.9"]

steps:
- uses: actions/checkout@v4
Expand All @@ -45,10 +45,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11
python-version: 3.12
- name: Install dependencies
run: |
pip install -U pip setuptools wheel
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -1,11 +1,21 @@
# Version history


## 3.1.1

### Bugfixes

- Make sure that schema is open by default: only check existing fields
- Add `--mypy-schema-closed` option to check schemas with no extra fields


## 3.1.0

### Features

- Add `python3.12` support
- Add `mypy@1.8.0` support
- Add schema definition


## 3.0.0
Expand Down
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -185,13 +185,18 @@ mypy-tests:
--mypy-ini-file=MYPY_INI_FILE
Which `.ini` file to use as a default config for tests.
Incompatible with `--mypy-pyproject-toml-file`
--mypy-same-process Run in the same process. Useful for debugging,
--mypy-same-process
Run in the same process. Useful for debugging,
will create problems with import cache
--mypy-extension-hook=MYPY_EXTENSION_HOOK
Fully qualified path to the extension hook function,
in case you need custom yaml keys. Has to be top-level
--mypy-only-local-stub
mypy will ignore errors from site-packages
--mypy-closed-schema
Use closed schema to validate YAML test cases,
which won't allow any extra keys
(does not work well with `--mypy-extension-hook`)
```

Expand Down
25 changes: 15 additions & 10 deletions pytest_mypy_plugins/collect.py
Expand Up @@ -31,27 +31,27 @@
from pytest_mypy_plugins.item import YamlTestItem


SCHEMA = json.loads((pathlib.Path(__file__).parent / "schema.json").read_text("utf8"))
SCHEMA["items"]["properties"]["__line__"] = {
"type": "integer",
"description": "Line number where the test starts (`pytest-mypy-plugins` internal)",
}


@dataclass
class File:
path: str
content: str


def validate_schema(data: Any) -> None:
def validate_schema(data: Any, *, is_closed: bool = False) -> None:
"""Validate the schema of the file-under-test."""
# Unfortunately, yaml.safe_load() returns Any,
# so we make our intention explicit here.
if not isinstance(data, list):
raise TypeError(f"Test file has to be YAML list, got {type(data)!r}.")

jsonschema.validate(instance=data, schema=SCHEMA)
schema = json.loads((pathlib.Path(__file__).parent / "schema.json").read_text("utf8"))
schema["items"]["properties"]["__line__"] = {
"type": "integer",
"description": "Line number where the test starts (`pytest-mypy-plugins` internal)",
}
schema["items"]["additionalProperties"] = not is_closed

jsonschema.validate(instance=data, schema=schema)


def parse_test_files(test_files: List[Dict[str, Any]]) -> List[File]:
Expand Down Expand Up @@ -114,7 +114,7 @@ def collect(self) -> Iterator["YamlTestItem"]:
if parsed_file is None:
return

validate_schema(parsed_file)
validate_schema(parsed_file, is_closed=self.config.option.mypy_closed_schema)

if not isinstance(parsed_file, list):
raise ValueError(f"Test file has to be YAML list, got {type(parsed_file)!r}.")
Expand Down Expand Up @@ -220,3 +220,8 @@ def pytest_addoption(parser: Parser) -> None:
action="store_true",
help="mypy will ignore errors from site-packages",
)
group.addoption(
"--mypy-closed-schema",
action="store_true",
help="Use closed schema to validate YAML test cases, which won't allow any extra keys (does not work well with `--mypy-extension-hook`)",
)
11 changes: 1 addition & 10 deletions pytest_mypy_plugins/schema.json
Expand Up @@ -6,7 +6,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"case": {
"type": "string",
Expand Down Expand Up @@ -130,15 +130,6 @@
"type": "boolean",
"description": "Allow regular expressions in comments to be matched against actual output. _See pytest_mypy_plugins/tests/test-regex_assertions.yml for examples_",
"default": false
},
"reveal_type": {
"description": "Shorthand for\n\n```yaml\n main: |\n reveal_type({{ reveal_type }})\n```\n\nMust be a syntactically valid Python expression.\n",
"examples": [
"1",
1,
true,
"sys.version_info"
]
}
},
"required": [
Expand Down
16 changes: 16 additions & 0 deletions pytest_mypy_plugins/tests/test_input_schema.py
Expand Up @@ -40,3 +40,19 @@ def test_mypy_config_is_not_an_object() -> None:
assert (
ex.value.message == "[{'force_uppercase_builtins': True}, {'force_union_syntax': True}] is not of type 'string'"
)


def test_closed_schema() -> None:
with pytest.raises(jsonschema.exceptions.ValidationError) as ex:
validate_schema(
[
{
"case": "mypy_config_is_not_an_object",
"main": "False",
"extra_field": 1,
}
],
is_closed=True,
)

assert ex.value.message == "Additional properties are not allowed ('extra_field' was unexpected)"
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -17,7 +17,7 @@

setup(
name="pytest-mypy-plugins",
version="3.1.0",
version="3.1.1",
description="pytest plugin for writing tests for mypy plugins",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 63ed231

Please sign in to comment.