Coverage for tests / unit / validate_type / test_validate_type_GENERATED.py: 79%
223 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 21:32 -0600
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-18 21:32 -0600
1# DO NOT EDIT, GENERATED FILE
2from __future__ import annotations
4import typing
5from typing import Any, Optional, Union
7import pytest
9from muutils.validate_type import IncorrectTypeException, validate_type
12# Tests for basic types and common use cases
13@pytest.mark.parametrize(
14 "value, expected_type, expected_result",
15 [
16 (42, int, True),
17 (3.14, float, True),
18 (5, int, True),
19 (5.0, int, False),
20 ("hello", str, True),
21 (True, bool, True),
22 (None, type(None), True),
23 (None, int, False),
24 ([1, 2, 3], list, True),
25 ([1, 2, 3], list, True),
26 ({"a": 1, "b": 2}, dict, True),
27 ({"a": 1, "b": 2}, dict, True),
28 ({1, 2, 3}, set, True),
29 ({1, 2, 3}, set, True),
30 ((1, 2, 3), tuple, True),
31 ((1, 2, 3), tuple, True),
32 (b"bytes", bytes, True),
33 (b"bytes", str, False),
34 ("3.14", float, False),
35 ("hello", Any, True),
36 (5, Any, True),
37 (3.14, Any, True),
38 # ints
39 (int(0), int, True),
40 (int(1), int, True),
41 (int(-1), int, True),
42 # bools
43 (True, bool, True),
44 (False, bool, True),
45 ],
46)
47def test_validate_type_basic(value, expected_type, expected_result):
48 try:
49 assert validate_type(value, expected_type) == expected_result
50 except Exception as e:
51 raise Exception(
52 f"{value = }, {expected_type = }, {expected_result = }, {e}"
53 ) from e
56@pytest.mark.parametrize(
57 "value",
58 [
59 42,
60 "hello",
61 3.14,
62 True,
63 None,
64 [1, 2, 3],
65 {"a": 1, "b": 2},
66 {1, 2, 3},
67 (1, 2, 3),
68 b"bytes",
69 "3.14",
70 ],
71)
72def test_validate_type_any(value):
73 try:
74 assert validate_type(value, Any)
75 except Exception as e:
76 raise Exception(f"{value = }, expected `Any`, {e}") from e
79@pytest.mark.parametrize(
80 "value, expected_type, expected_result",
81 [
82 (42, Union[int, str], True),
83 ("hello", Union[int, str], True),
84 (3.14, Union[int, float], True),
85 (True, Union[int, str], True),
86 (None, Union[int, None], True),
87 (None, Union[int, str], False),
88 (5, Union[int, str], True),
89 (5.0, Union[int, str], False),
90 ("hello", Union[int, str], True),
91 (5, typing.Union[int, str], True),
92 ("hello", typing.Union[int, str], True),
93 (5.0, typing.Union[int, str], False),
94 (5, Union[int, str], True),
95 ("hello", Union[int, str], True),
96 (5.0, Union[int, str], False),
97 ],
98)
99def test_validate_type_union(value, expected_type, expected_result):
100 try:
101 assert validate_type(value, expected_type) == expected_result
102 except Exception as e:
103 raise Exception(
104 f"{value = }, {expected_type = }, {expected_result = }, {e}"
105 ) from e
108@pytest.mark.parametrize(
109 "value, expected_type, expected_result",
110 [
111 (42, Optional[int], True),
112 ("hello", Optional[int], False),
113 (3.14, Optional[int], False),
114 ([1], Optional[list[int]], True),
115 (None, Optional[int], True),
116 (None, Optional[str], True),
117 (None, Optional[int], True),
118 (None, Optional[None], True),
119 (None, Optional[list[dict[str, int]]], True),
120 ],
121)
122def test_validate_type_optional(value, expected_type, expected_result):
123 try:
124 assert validate_type(value, expected_type) == expected_result
125 except Exception as e:
126 raise Exception(
127 f"{value = }, {expected_type = }, {expected_result = }, {e}"
128 ) from e
131@pytest.mark.parametrize(
132 "value, expected_type, expected_result",
133 [
134 (42, list[int], False),
135 ([1, 2, 3], list[int], True),
136 ([1, 2, 3], list[str], False),
137 (["a", "b", "c"], list[str], True),
138 ([1, "a", 3], list[int], False),
139 (42, list[int], False),
140 ([1, 2, 3], list[int], True),
141 ([1, "2", 3], list[int], False),
142 ],
143)
144def test_validate_type_list(value, expected_type, expected_result):
145 try:
146 assert validate_type(value, expected_type) == expected_result
147 except Exception as e:
148 raise Exception(
149 f"{value = }, {expected_type = }, {expected_result = }, {e}"
150 ) from e
153@pytest.mark.parametrize(
154 "value, expected_type, expected_result",
155 [
156 (42, dict[str, int], False),
157 ({"a": 1, "b": 2}, dict[str, int], True),
158 ({"a": 1, "b": 2}, dict[int, str], False),
159 (42, dict[str, int], False),
160 ({"a": 1, "b": 2}, dict[str, int], True),
161 ({"a": 1, "b": 2}, dict[int, str], False),
162 ({1: "a", 2: "b"}, dict[int, str], True),
163 ({1: "a", 2: "b"}, dict[str, int], False),
164 ({"a": 1, "b": "c"}, dict[str, int], False),
165 ([("a", 1), ("b", 2)], dict[str, int], False),
166 ({"key": "value"}, dict[str, str], True),
167 ({"key": 2}, dict[str, str], False),
168 ({"key": 2}, dict[str, int], True),
169 ({"key": 2.0}, dict[str, int], False),
170 ({"a": 1, "b": 2}, dict[str, int], True),
171 ({"a": 1, "b": "2"}, dict[str, int], False),
172 ],
173)
174def test_validate_type_dict(value, expected_type, expected_result):
175 try:
176 assert validate_type(value, expected_type) == expected_result
177 except Exception as e:
178 raise Exception(
179 f"{value = }, {expected_type = }, {expected_result = }, {e}"
180 ) from e
183@pytest.mark.parametrize(
184 "value, expected_type, expected_result",
185 [
186 (42, set[int], False),
187 ({1, 2, 3}, set[int], True),
188 (42, set[int], False),
189 ({1, 2, 3}, set[int], True),
190 ({1, 2, 3}, set[str], False),
191 ({"a", "b", "c"}, set[str], True),
192 ({1, "a", 3}, set[int], False),
193 (42, set[int], False),
194 ({1, 2, 3}, set[int], True),
195 ({1, "2", 3}, set[int], False),
196 ([1, 2, 3], set[int], False),
197 ("hello", set[str], False),
198 ],
199)
200def test_validate_type_set(value, expected_type, expected_result):
201 try:
202 assert validate_type(value, expected_type) == expected_result
203 except Exception as e:
204 raise Exception(
205 f"{value = }, {expected_type = }, {expected_result = }, {e}"
206 ) from e
209@pytest.mark.parametrize(
210 "value, expected_type, expected_result",
211 [
212 (42, tuple[int, str], False),
213 ((1, "a"), tuple[int, str], True),
214 (42, tuple[int, str], False),
215 ((1, "a"), tuple[int, str], True),
216 ((1, 2), tuple[int, str], False),
217 ((1, 2), tuple[int, int], True),
218 ((1, 2, 3), tuple[int, int], False),
219 ((1, "a", 3.14), tuple[int, str, float], True),
220 (("a", "b", "c"), tuple[str, str, str], True),
221 ((1, "a", 3.14), tuple[int, str], False),
222 ((1, "a", 3.14), tuple[int, str, float], True),
223 ([1, "a", 3.14], tuple[int, str, float], False),
224 (
225 (1, "a", 3.14, "b", True, None, (1, 2, 3)),
226 # no idea why this throws type error, only locally, and only for the generated modern types
227 tuple[ # type: ignore[misc]
228 int, str, float, str, bool, None, tuple[int, int, int]
229 ],
230 True,
231 ),
232 ],
233)
234def test_validate_type_tuple(value, expected_type, expected_result):
235 try:
236 assert validate_type(value, expected_type) == expected_result
237 except Exception as e:
238 raise Exception(
239 f"{value = }, {expected_type = }, {expected_result = }, {e}"
240 ) from e
243@pytest.mark.parametrize(
244 "value, expected_type, expected_result",
245 [
246 # basic string literals
247 ("a", typing.Literal["a", "b"], True),
248 ("b", typing.Literal["a", "b"], True),
249 ("c", typing.Literal["a", "b"], False),
250 # basic int literals
251 (1, typing.Literal[1, 2, 3], True),
252 (4, typing.Literal[1, 2, 3], False),
253 # single-value literal
254 ("a", typing.Literal["a"], True),
255 ("b", typing.Literal["a"], False),
256 # mixed-type literal
257 ("a", typing.Literal["a", 1], True),
258 (1, typing.Literal["a", 1], True),
259 (2, typing.Literal["a", 1], False),
260 # None in literal
261 (None, typing.Literal[None, "x"], True),
262 ("x", typing.Literal[None, "x"], True),
263 (1, typing.Literal[None, "x"], False),
264 # bool literal (note: True == 1 in Python, so True in Literal[True] is True)
265 (True, typing.Literal[True], True),
266 (False, typing.Literal[True], False),
267 # Literal nested in Optional
268 (None, typing.Optional[typing.Literal["a", "b"]], True),
269 ("a", typing.Optional[typing.Literal["a", "b"]], True),
270 ("c", typing.Optional[typing.Literal["a", "b"]], False),
271 # Literal nested in Union
272 ("a", typing.Union[typing.Literal["a", "b"], int], True),
273 (1, typing.Union[typing.Literal["a", "b"], int], True),
274 ("c", typing.Union[typing.Literal["a", "b"], int], False),
275 # List[Literal[...]]
276 (["a", "b"], list[typing.Literal["a", "b", "c"]], True),
277 (["a", "d"], list[typing.Literal["a", "b", "c"]], False),
278 ([], list[typing.Literal["a", "b"]], True),
279 # Dict[str, Literal[...]]
280 ({"k": "a"}, dict[str, typing.Literal["a", "b"]], True),
281 ({"k": "c"}, dict[str, typing.Literal["a", "b"]], False),
282 ({}, dict[str, typing.Literal["a", "b"]], True),
283 # Dict[Literal[...], int]
284 ({"x": 1, "y": 2}, dict[typing.Literal["x", "y"], int], True),
285 ({"x": 1, "z": 2}, dict[typing.Literal["x", "y"], int], False),
286 ({}, dict[typing.Literal["x", "y"], int], True),
287 # Set[Literal[...]]
288 ({"a", "b"}, set[typing.Literal["a", "b", "c"]], True),
289 ({"a", "d"}, set[typing.Literal["a", "b", "c"]], False),
290 (set(), set[typing.Literal["a", "b"]], True),
291 # Tuple[Literal[...], Literal[...]]
292 (
293 ("fast", 1),
294 tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]],
295 True,
296 ),
297 (
298 ("fast", 3),
299 tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]],
300 False,
301 ),
302 (
303 ("bad", 1),
304 tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]],
305 False,
306 ),
307 # bool/int Literal ambiguity: mirrors Python's bool-is-int semantics (True == 1, False == 0)
308 (True, typing.Literal[1], True),
309 (False, typing.Literal[0], True),
310 (1, typing.Literal[True], True),
311 (0, typing.Literal[False], True),
312 ],
313)
314def test_validate_type_literal(value, expected_type, expected_result):
315 try:
316 assert validate_type(value, expected_type) == expected_result
317 except Exception as e:
318 raise Exception(
319 f"{value = }, {expected_type = }, {expected_result = }, {e}"
320 ) from e
323def test_validate_type_literal_do_except():
324 assert validate_type("a", typing.Literal["a", "b"], do_except=True)
325 with pytest.raises(IncorrectTypeException):
326 validate_type("c", typing.Literal["a", "b"], do_except=True)
329def test_validate_type_list_do_except():
330 # valid list — no raise
331 assert validate_type([1, 2, 3], list[int], do_except=True)
332 assert validate_type(
333 ["a", "b"], list[typing.Literal["a", "b"]], do_except=True
334 )
335 # bad element — must raise, not return False
336 with pytest.raises(IncorrectTypeException):
337 validate_type([1, "bad"], list[int], do_except=True)
338 with pytest.raises(IncorrectTypeException):
339 validate_type(
340 ["a", "bad"], list[typing.Literal["a", "b"]], do_except=True
341 )
344@pytest.mark.parametrize(
345 "value, expected_type",
346 [
347 (43, typing.Callable),
348 (lambda x: x, typing.Callable),
349 (42, typing.Callable[[], None]),
350 (42, typing.Callable[[int, str], list]),
351 ],
352)
353def test_validate_type_unsupported_type_hint(value, expected_type):
354 with pytest.raises(NotImplementedError):
355 validate_type(value, expected_type)
356 print(f"Failed to except: {value = }, {expected_type = }")
359@pytest.mark.parametrize(
360 "value, expected_type, expected_result",
361 [
362 ([1, 2, 3], list[int], True),
363 (["a", "b", "c"], list[str], True),
364 ([1, "a", 3], list[int], False),
365 ([1, 2, [3, 4]], list[Union[int, list[int]]], True),
366 ([(1, 2), (3, 4)], list[tuple[int, int]], True),
367 ([(1, 2), (3, "4")], list[tuple[int, int]], False),
368 ({1: [1, 2], 2: [3, 4]}, dict[int, list[int]], True),
369 ({1: [1, 2], 2: [3, "4"]}, dict[int, list[int]], False),
370 ],
371)
372def test_validate_type_collections(value, expected_type, expected_result):
373 try:
374 assert validate_type(value, expected_type) == expected_result
375 except Exception as e:
376 raise Exception(
377 f"{value = }, {expected_type = }, {expected_result = }, {e}"
378 ) from e
381@pytest.mark.parametrize(
382 "value, expected_type, expected_result",
383 [
384 # empty lists
385 ([], list[int], True),
386 ([], list[dict], True),
387 (
388 [],
389 list[tuple[dict[tuple, str], str, None]],
390 True,
391 ),
392 # empty dicts
393 ({}, dict[str, int], True),
394 ({}, dict[str, dict], True),
395 ({}, dict[str, dict[str, int]], True),
396 ({}, dict[str, dict[str, int]], True),
397 # empty sets
398 (set(), set[int], True),
399 (set(), set[dict], True),
400 (
401 set(),
402 set[tuple[dict[tuple, str], str, None]],
403 True,
404 ),
405 # empty tuple
406 (tuple(), tuple, True),
407 # empty string
408 ("", str, True),
409 # empty bytes
410 (b"", bytes, True),
411 # None
412 (None, type(None), True),
413 # bools are ints, ints are not floats
414 (True, int, True),
415 (False, int, True),
416 (True, float, False),
417 (False, float, False),
418 (1, int, True),
419 (0, int, True),
420 (1, float, False),
421 (0, float, False),
422 (0, bool, False),
423 (1, bool, False),
424 # weird floats
425 (float("nan"), float, True),
426 (float("inf"), float, True),
427 (float("-inf"), float, True),
428 (float(0), float, True),
429 # list/tuple
430 ([1], tuple[int, int], False),
431 ((1, 2), list[int], False),
432 ],
433)
434def test_validate_type_edge_cases(value, expected_type, expected_result):
435 try:
436 assert validate_type(value, expected_type) == expected_result
437 except Exception as e:
438 raise Exception(
439 f"{value = }, {expected_type = }, {expected_result = }, {e}"
440 ) from e
443@pytest.mark.parametrize(
444 "value, expected_type, expected_result",
445 [
446 (42, list[int], False),
447 ([1, 2, 3], int, False),
448 (3.14, tuple[float], False),
449 (3.14, tuple[float, float], False),
450 (3.14, tuple[bool, str], False),
451 (False, tuple[bool, str], False),
452 (False, tuple[bool], False),
453 ((False,), tuple[bool], True),
454 (("abc",), tuple[str], True),
455 ("test-dict", dict[str, int], False),
456 ("test-dict", dict, False),
457 ],
458)
459def test_validate_type_wrong_type(value, expected_type, expected_result):
460 try:
461 assert validate_type(value, expected_type) == expected_result
462 except Exception as e:
463 raise Exception(
464 f"{value = }, {expected_type = }, {expected_result = }, {e}"
465 ) from e
468def test_validate_type_complex():
469 assert validate_type([1, 2, [3, 4]], list[Union[int, list[int]]])
470 assert validate_type(
471 {"a": 1, "b": {"c": 2}}, dict[str, Union[int, dict[str, int]]]
472 )
473 assert validate_type({1, (2, 3)}, set[Union[int, tuple[int, int]]])
474 assert validate_type((1, ("a", "b")), tuple[int, tuple[str, str]])
475 assert validate_type([{"key": "value"}], list[dict[str, str]])
476 assert validate_type([{"key": 2}], list[dict[str, str]]) is False
477 assert validate_type([[1, 2], [3, 4]], list[list[int]])
478 assert validate_type([[1, 2], [3, "4"]], list[list[int]]) is False
479 assert validate_type([(1, 2), (3, 4)], list[tuple[int, int]])
480 assert (
481 validate_type([(1, 2), (3, "4")], list[tuple[int, int]]) is False
482 )
483 assert validate_type({1: "one", 2: "two"}, dict[int, str])
484 assert validate_type({1: "one", 2: 2}, dict[int, str]) is False
485 assert validate_type([(1, "one"), (2, "two")], list[tuple[int, str]])
486 assert (
487 validate_type([(1, "one"), (2, 2)], list[tuple[int, str]])
488 is False
489 )
490 assert validate_type({1: [1, 2], 2: [3, 4]}, dict[int, list[int]])
491 assert (
492 validate_type({1: [1, 2], 2: [3, "4"]}, dict[int, list[int]])
493 is False
494 )
495 assert validate_type([(1, "a"), (2, "b")], list[tuple[int, str]])
496 assert (
497 validate_type([(1, "a"), (2, 2)], list[tuple[int, str]]) is False
498 )
501@pytest.mark.parametrize(
502 "value, expected_type, expected_result",
503 [
504 ([[[[1]]]], list[list[list[list[int]]]], True),
505 ([[[[1]]]], list[list[list[list[str]]]], False),
506 (
507 {"a": {"b": {"c": 1}}},
508 dict[str, dict[str, dict[str, int]]],
509 True,
510 ),
511 (
512 {"a": {"b": {"c": 1}}},
513 dict[str, dict[str, dict[str, str]]],
514 False,
515 ),
516 ({1, 2, 3}, set[int], True),
517 ({1, 2, 3}, set[str], False),
518 (
519 ((1, 2), (3, 4)),
520 tuple[tuple[int, int], tuple[int, int]],
521 True,
522 ),
523 (
524 ((1, 2), (3, 4)),
525 tuple[tuple[int, int], tuple[int, str]],
526 False,
527 ),
528 ],
529)
530def test_validate_type_nested(value, expected_type, expected_result):
531 try:
532 assert validate_type(value, expected_type) == expected_result
533 except Exception as e:
534 raise Exception(
535 f"{value = }, {expected_type = }, {expected_result = }, {e}"
536 ) from e
539def test_validate_type_inheritance():
540 class Parent:
541 def __init__(self, a: int, b: str):
542 self.a: int = a
543 self.b: str = b
545 class Child(Parent):
546 def __init__(self, a: int, b: str):
547 self.a: int = 2 * a
548 self.b: str = b
550 assert validate_type(Parent(1, "a"), Parent)
551 validate_type(Child(1, "a"), Parent, do_except=True)
552 assert validate_type(Child(1, "a"), Child)
553 assert not validate_type(Parent(1, "a"), Child)
555 with pytest.raises(IncorrectTypeException):
556 validate_type(Parent(1, "a"), Child, do_except=True)
559def test_validate_type_class():
560 class Parent:
561 def __init__(self, a: int, b: str):
562 self.a: int = a
563 self.b: str = b
565 class Child(Parent):
566 def __init__(self, a: int, b: str):
567 self.a: int = 2 * a
568 self.b: str = b
570 assert validate_type(Parent, type)
571 assert validate_type(Child, type)
572 assert validate_type(Parent, typing.Type[Parent], do_except=True)
573 assert validate_type(Child, typing.Type[Child])
574 assert not validate_type(Parent, typing.Type[Child])
576 assert validate_type(Child, typing.Union[typing.Type[Child], typing.Type[Parent]])
577 assert validate_type(Child, typing.Union[typing.Type[Child], int])
580@pytest.mark.skip(reason="Not implemented")
581def test_validate_type_class_union():
582 class Parent:
583 def __init__(self, a: int, b: str):
584 self.a: int = a
585 self.b: str = b
587 class Child(Parent):
588 def __init__(self, a: int, b: str):
589 self.a: int = 2 * a
590 self.b: str = b
592 class Other:
593 def __init__(self, x: int, y: str):
594 self.x: int = x
595 self.y: str = y
597 assert validate_type(Child, typing.Type[typing.Union[Child, Parent]])
598 assert validate_type(Child, typing.Type[typing.Union[Child, Other]])
599 assert validate_type(Parent, typing.Type[typing.Union[Child, Other]])
600 assert validate_type(Parent, typing.Type[typing.Union[Parent, Other]])
603def test_validate_type_aliases():
604 AliasInt = int
605 AliasStr = str
606 AliasListInt = list[int]
607 AliasListStr = list[str]
608 AliasDictIntStr = dict[int, str]
609 AliasDictStrInt = dict[str, int]
610 AliasTupleIntStr = tuple[int, str]
611 AliasTupleStrInt = tuple[str, int]
612 AliasSetInt = set[int]
613 AliasSetStr = set[str]
614 AliasUnionIntStr = typing.Union[int, str]
615 AliasUnionStrInt = typing.Union[str, int]
616 AliasOptionalInt = typing.Optional[int]
617 AliasOptionalStr = typing.Optional[str]
618 AliasOptionalListInt = typing.Optional[list[int]]
619 AliasDictStrListInt = dict[str, list[int]]
621 assert validate_type(42, AliasInt)
622 assert not validate_type("42", AliasInt)
623 assert validate_type(42, AliasInt)
624 assert not validate_type("42", AliasInt)
625 assert validate_type("hello", AliasStr)
626 assert not validate_type(42, AliasStr)
627 assert validate_type([1, 2, 3], AliasListInt)
628 assert not validate_type([1, "2", 3], AliasListInt)
629 assert validate_type(["hello", "world"], AliasListStr)
630 assert not validate_type(["hello", 42], AliasListStr)
631 assert validate_type({1: "a", 2: "b"}, AliasDictIntStr)
632 assert not validate_type({1: 2, 3: 4}, AliasDictIntStr)
633 assert validate_type({"one": 1, "two": 2}, AliasDictStrInt)
634 assert not validate_type({1: "one", 2: "two"}, AliasDictStrInt)
635 assert validate_type((1, "a"), AliasTupleIntStr)
636 assert not validate_type(("a", 1), AliasTupleIntStr)
637 assert validate_type(("a", 1), AliasTupleStrInt)
638 assert not validate_type((1, "a"), AliasTupleStrInt)
639 assert validate_type({1, 2, 3}, AliasSetInt)
640 assert not validate_type({1, "two", 3}, AliasSetInt)
641 assert validate_type({"one", "two"}, AliasSetStr)
642 assert not validate_type({"one", 2}, AliasSetStr)
643 assert validate_type(42, AliasUnionIntStr)
644 assert validate_type("hello", AliasUnionIntStr)
645 assert not validate_type(3.14, AliasUnionIntStr)
646 assert validate_type("hello", AliasUnionStrInt)
647 assert validate_type(42, AliasUnionStrInt)
648 assert not validate_type(3.14, AliasUnionStrInt)
649 assert validate_type(42, AliasOptionalInt)
650 assert validate_type(None, AliasOptionalInt)
651 assert not validate_type("42", AliasOptionalInt)
652 assert validate_type("hello", AliasOptionalStr)
653 assert validate_type(None, AliasOptionalStr)
654 assert not validate_type(42, AliasOptionalStr)
655 assert validate_type([1, 2, 3], AliasOptionalListInt)
656 assert validate_type(None, AliasOptionalListInt)
657 assert not validate_type(["1", "2", "3"], AliasOptionalListInt)
658 assert validate_type({"key": [1, 2, 3]}, AliasDictStrListInt)
659 assert not validate_type({"key": [1, "2", 3]}, AliasDictStrListInt)