Skip to content

Commit 3e1c823

Browse files
luca.siligatoLuca Callocchia
authored andcommitted
Issue #2259: Added test to validate non-class type hint
1 parent a4143ae commit 3e1c823

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

ai-packages/embedding-modules/tests/test_chunk_arguments.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
#
1717

18-
from typing import Union
18+
from typing import Any, Dict, List, Literal, Optional, Union
1919

2020
from app.utils.chunk_arguments import build_chunk_arguments, coerce_argument
2121

@@ -82,3 +82,41 @@ def test_coerce_argument_rejects_non_int_like():
8282
accepted, value = coerce_argument("abc", int)
8383
assert accepted is False
8484
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

Comments
 (0)