|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | # |
17 | 17 |
|
18 | | -from typing import Union |
| 18 | +from typing import Any, Dict, List, Literal, Optional, Union |
19 | 19 |
|
20 | 20 | from app.utils.chunk_arguments import build_chunk_arguments, coerce_argument |
21 | 21 |
|
@@ -82,3 +82,41 @@ def test_coerce_argument_rejects_non_int_like(): |
82 | 82 | accepted, value = coerce_argument("abc", int) |
83 | 83 | assert accepted is False |
84 | 84 | assert value is None |
| 85 | + |
| 86 | + |
| 87 | +# Hints that raise TypeError when used as the second isinstance() argument, |
| 88 | +# mirroring real chonkie chunker signatures (SemanticChunker, SentenceChunker, |
| 89 | +# TokenChunker, LateChunker, NeuralChunker...). |
| 90 | +class _Proto: # non-runtime-checkable protocol stand-in |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +UNVERIFIABLE_HINTS = [ |
| 95 | + Union[str, List[str]], # delim |
| 96 | + Optional[Literal["prev", "next"]], # include_delim |
| 97 | + Dict[str, Any], # kwargs |
| 98 | + Any, # kwargs / model |
| 99 | + Union[str, Any], # model |
| 100 | +] |
| 101 | + |
| 102 | + |
| 103 | +def test_unverifiable_hint_passes_value_through(): |
| 104 | + """A hint isinstance() cannot evaluate must not crash; value passes through.""" |
| 105 | + for hint in UNVERIFIABLE_HINTS: |
| 106 | + accepted, value = coerce_argument("next", hint) |
| 107 | + assert accepted is True |
| 108 | + assert value == "next" |
| 109 | + |
| 110 | + |
| 111 | +def test_build_chunk_arguments_survives_unverifiable_hint(): |
| 112 | + """Regression: a config key with a subscripted-generic hint must not raise.""" |
| 113 | + signature = { |
| 114 | + "chunk_size": int, |
| 115 | + "include_delim": Optional[Literal["prev", "next"]], |
| 116 | + "delim": Union[str, List[str]], |
| 117 | + } |
| 118 | + args = build_chunk_arguments( |
| 119 | + {"chunk_size": 512.0, "include_delim": "next", "delim": "\n"}, signature |
| 120 | + ) |
| 121 | + assert args == {"chunk_size": 512, "include_delim": "next", "delim": "\n"} |
| 122 | + assert type(args["chunk_size"]) is int |
0 commit comments