Skip to content

Commit

Permalink
More work trying to appease the mypy overlord
Browse files Browse the repository at this point in the history
  • Loading branch information
56kyle committed Aug 20, 2023
1 parent 0cef4c7 commit d434489
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
60 changes: 45 additions & 15 deletions src/pytest_static/parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from dataclasses import field
from enum import Enum
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Dict
Expand All @@ -22,11 +23,16 @@
from typing import get_args
from typing import get_origin

from _pytest.mark import Mark
from _pytest.python import Metafunc

from pytest_static.type_sets import PREDEFINED_TYPE_SETS


if TYPE_CHECKING:
from _pytest.scope import _ScopeName


T = TypeVar("T")


Expand Down Expand Up @@ -100,32 +106,45 @@ def _instantiate_from_signature(
"""Returns a tuple of all possible instances of the primary_type."""
signature: inspect.Signature = inspect.signature(self.primary_type)
if len(signature.parameters) > 1:
return tuple(
self._instantiate_expanded(pc) for pc in parameter_combinations
return self._instantiate_combinations_using_expanded(
parameter_combinations=parameter_combinations
)
return tuple(
self._instantiate_not_expanded(pc) for pc in parameter_combinations
return self._instantiate_combinations_using_not_expanded(
parameter_combinations=parameter_combinations
)

def _instantiate_from_trial_and_error(
self, parameter_combinations: List[Tuple[Any, ...]]
) -> Tuple[T, ...]:
"""Returns a tuple of all possible instances of the primary_type."""
try:
return tuple(
self._instantiate_expanded(pc) for pc in parameter_combinations
return self._instantiate_combinations_using_expanded(
parameter_combinations=parameter_combinations
)
except TypeError:
return tuple(
self._instantiate_not_expanded(pc) for pc in parameter_combinations
return self._instantiate_combinations_using_not_expanded(
parameter_combinations=parameter_combinations
)

def _instantiate_combinations_using_expanded(
self, parameter_combinations: List[Tuple[Any, ...]]
) -> Tuple[T, ...]:
"""Returns a tuple of all possible instances of the primary_type."""
return tuple(self._instantiate_expanded(pc) for pc in parameter_combinations)

def _instantiate_combinations_using_not_expanded(
self, parameter_combinations: List[Tuple[Any, ...]]
) -> Tuple[T, ...]:
"""Returns a tuple of all possible instances of the primary_type."""
return tuple(
self._instantiate_not_expanded(pc) for pc in parameter_combinations
)

def _instantiate_expanded(self, combination: Tuple[Any, ...]) -> T:
"""Returns an instance of the primary_type using the combination provided."""
if self.primary_type is dict:
return {combination[0]: combination[1]}
else:
return self.primary_type(*combination)
return self.primary_type([combination])
return self.primary_type(*combination)

def _instantiate_not_expanded(self, combination: Tuple[Any, ...]) -> T:
"""Returns an instance of the primary_type using the combination provided."""
Expand All @@ -150,9 +169,13 @@ def parametrize_types(
metafunc: Metafunc,
argnames: Union[str, Sequence[str]],
argtypes: List[Type[T]],
ids: Optional[Union[Sequence[str], Callable]] = None,
*args,
**kwargs
indirect: Union[bool, Sequence[str]] = False,
ids: Optional[
Union[Iterable[Optional[object]], Callable[[Any], Optional[object]]]
] = None,
scope: "Optional[_ScopeName]" = None,
*,
_param_mark: Optional[Mark] = None,
) -> None:
"""Parametrizes the provided argnames with the provided argtypes."""
argnames = _ensure_sequence(argnames)
Expand All @@ -167,7 +190,14 @@ def parametrize_types(
if ids is None:
ids = [", ".join(map(repr, ic)) for ic in instance_combinations]

metafunc.parametrize(argnames, instance_combinations, ids=ids, *args, **kwargs)
metafunc.parametrize(
argnames=argnames,
argvalues=instance_combinations,
indirect=indirect,
ids=ids,
scope=scope,
_param_mark=_param_mark,
)


def get_all_possible_type_instances(
Expand Down
Empty file.
10 changes: 5 additions & 5 deletions tests/unit_tests/test_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ def test_get_instances_with_nested(self) -> None:
def test_get_instances_with_multiple(self, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(PREDEFINED_TYPE_SETS, int, {1, 2})
monkeypatch.setitem(PREDEFINED_TYPE_SETS, str, {"a", "b"})
expected_instances: Tuple[List[int, str], ...] = (
expected_instances: Tuple[List[Union[int, str]], ...] = (
[1, "a"],
[1, "b"],
[2, "a"],
[2, "b"],
)
expanded_type = ExpandedType(list, (int, str))
expanded_instances: Tuple[Tuple[Any, ...], ...] = expanded_type.get_instances()
expanded_type: ExpandedType[List[Any]] = ExpandedType(list, (int, str))
expanded_instances: Tuple[List[Any], ...] = expanded_type.get_instances()
for instance in expected_instances:
assert instance in expanded_instances

def test_get_instances_with_multiple_nested(self, monkeypatch: MonkeyPatch) -> None:
monkeypatch.setitem(PREDEFINED_TYPE_SETS, int, {1, 2})
monkeypatch.setitem(PREDEFINED_TYPE_SETS, str, {"a", "b"})
expected_instances: Tuple[List[List[int], str], ...] = (
expected_instances: Tuple[List[Union[List[int], str]], ...] = (
[[1], "a"],
[[1], "b"],
[[2], "a"],
[[2], "b"],
)
expanded_type = ExpandedType(list, (ExpandedType(list, (int,)), str))
expanded_instances: Tuple[Tuple[Any, ...], ...] = expanded_type.get_instances()
expanded_instances: Tuple[List[Any], ...] = expanded_type.get_instances()
for instance in expected_instances:
assert instance in expanded_instances

Expand Down

0 comments on commit d434489

Please sign in to comment.