Files
lix/tests/functional2/testlib/test_utils.py
T
helle db56d236dd tests/functional2: fixes prerequisite to ruff upgrade to 0.11.10
Most of these are simple fixes and clarifications. One set of fixes will
come in the commit that actually upgrades nixpkgs and hence ruff as it
will otherwise cause errors here.

Change-Id: Ie857da0f6cf728478700ec2d24cf518f8c7b7815
2025-06-13 12:51:34 +02:00

88 lines
2.1 KiB
Python

from collections.abc import Callable, Generator
from typing import Literal, Any
import pytest
from functional2.testlib.utils import is_value_of_type
def test_list_type_valid():
assert is_value_of_type([1, 2, 3], list[int])
def test_list_type_valid_multi_type():
assert is_value_of_type([1, "a", 2], list[int | str])
def test_list_type_invalid_single_fail():
assert not is_value_of_type([1, 2, "a"], list[int])
def test_list_type_invalid_all_wrong():
assert not is_value_of_type([1, 2, 3], list[str])
def test_list_type_nested():
assert is_value_of_type([[1], [2, 3], [4]], list[list[int]])
def test_list_type_nested_single_invalid():
assert not is_value_of_type([[1], [2, 3], ["a"]], list[list[int]])
def test_weird_type_valid():
assert is_value_of_type(42, Literal[42])
def test_type_type():
class Foo: ...
assert is_value_of_type(Foo, type[Foo])
def test_type_doesnt_allow_generator():
def foo():
yield 1
with pytest.raises(ValueError, match=r"Unsupported expected_type.+"):
is_value_of_type(foo, Generator[int, None, None])
def test_type_doesnt_allow_callable():
def foo(a): # noqa: ANN001, ANN202 # dummy stuff within testing, nothing external
return "a" + str(a)
with pytest.raises(ValueError, match=r"Unsupported expected_type.+"):
is_value_of_type(foo, Callable[[int], str])
def test_type_allows_none():
assert is_value_of_type(None, type[None])
def test_doesnt_allow_generic():
class X[T]: ...
with pytest.raises(ValueError, match=r"Unsupported expected_type.+"):
is_value_of_type(0, X[int])
def test_type_dict_checks_valid():
d = {"a": 1, "b": 2}
assert is_value_of_type(d, dict[str, int])
def test_type_dict_key_invalid():
d = {"a": 1, 1: 2}
assert not is_value_of_type(d, dict[str, int])
def test_type_dict_value_invalid():
d = {"a": 1, "b": "2"}
assert not is_value_of_type(d, dict[str, int])
def test_type_allows_any():
assert is_value_of_type(1, Any)
assert is_value_of_type("a", Any)
assert is_value_of_type(lambda x: x, Any)