Skip to content

Commit

Permalink
Remove the need for make_pytest_item
Browse files Browse the repository at this point in the history
This stayed as long as it did to make the git diff a bit more useful
  • Loading branch information
delfick committed May 4, 2024
1 parent dd49e71 commit 94f19e2
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions pytest_mypy_plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import platform
import sys
from collections import defaultdict
from typing import Any, Callable, Dict, Iterator, List, Mapping, Union
from typing import Any, Dict, Iterator, List, Mapping, Union

import jsonschema
import pytest
Expand Down Expand Up @@ -97,16 +97,13 @@ class ItemDefinition:

# This is set when `from_yaml` returns all the parametrized, non skipped tests
item_params: Mapping[str, object] = dataclasses.field(default_factory=dict, init=False)
make_pytest_item: Callable[[pytest.Collector], pytest.Item] = dataclasses.field(init=False)

def __post_init__(self) -> None:
if not self.case.isidentifier():
raise ValueError(f"Invalid test name {self.case!r}, only '[a-zA-Z0-9_]' is allowed.")

@classmethod
def from_yaml(cls, data: List[Mapping[str, object]], *, is_closed: bool = False) -> Iterator["ItemDefinition"]:
from pytest_mypy_plugins.item import YamlTestItem

# Validate the shape of data so we can make reasonable assumptions
validate_schema(data, is_closed=is_closed)

Expand Down Expand Up @@ -154,23 +151,11 @@ def from_yaml(cls, data: List[Mapping[str, object]], *, is_closed: bool = False)

nxt = cls(**kwargs)
for params in parametrized:
self = dataclasses.replace(nxt)
self.item_params = params

if not self._skipped:
self.make_pytest_item = lambda parent: YamlTestItem.from_parent(
parent,
name=self.test_name,
files=self.all_files,
starting_lineno=self.starting_lineno,
environment_variables=self.extra_environment_variables,
disable_cache=self.disable_cache,
expected_output=self.expected_output,
parsed_test_data=self.raw_test,
mypy_config=self.additional_mypy_config,
expect_fail=self.expect_fail,
)
yield self
clone = dataclasses.replace(nxt)
clone.item_params = params

if not clone._skipped:
yield clone

@property
def _skipped(self) -> bool:
Expand Down Expand Up @@ -213,4 +198,17 @@ def additional_mypy_config(self) -> str:
return utils.render_template(template=self.mypy_config, data=self.item_params)

def pytest_item(self, parent: pytest.Collector) -> pytest.Item:
return self.make_pytest_item(parent)
from pytest_mypy_plugins.item import YamlTestItem

return YamlTestItem.from_parent(
parent,
name=self.test_name,
files=self.all_files,
starting_lineno=self.starting_lineno,
environment_variables=self.extra_environment_variables,
disable_cache=self.disable_cache,
expected_output=self.expected_output,
parsed_test_data=self.raw_test,
mypy_config=self.additional_mypy_config,
expect_fail=self.expect_fail,
)

0 comments on commit 94f19e2

Please sign in to comment.