|
1 |
| -from collections.abc import Sequence |
| 1 | +from types import NoneType |
2 | 2 | from typing import Union
|
3 | 3 |
|
| 4 | +from langflow.schema.data import Data |
4 | 5 | import pytest
|
5 | 6 | from pydantic import ValidationError
|
6 | 7 |
|
7 | 8 | from langflow.template import Input, Output
|
8 | 9 | from langflow.template.field.base import UNDEFINED
|
9 | 10 | from langflow.type_extraction.type_extraction import post_process_type
|
| 11 | +from collections.abc import Sequence as SequenceABC |
10 | 12 |
|
11 | 13 |
|
12 | 14 | @pytest.fixture(name="client", autouse=True)
|
@@ -40,11 +42,62 @@ def test_validate_type_class(self):
|
40 | 42 | assert input_obj.field_type == "int"
|
41 | 43 |
|
42 | 44 | def test_post_process_type_function(self):
|
| 45 | + # Basic types |
43 | 46 | assert set(post_process_type(int)) == {int}
|
| 47 | + assert set(post_process_type(float)) == {float} |
| 48 | + |
| 49 | + # List and Sequence types |
44 | 50 | assert set(post_process_type(list[int])) == {int}
|
| 51 | + assert set(post_process_type(SequenceABC[float])) == {float} |
| 52 | + |
| 53 | + # Union types |
45 | 54 | assert set(post_process_type(Union[int, str])) == {int, str}
|
46 |
| - assert set(post_process_type(Union[int, Sequence[str]])) == {int, str} |
47 |
| - assert set(post_process_type(Union[int, Sequence[int]])) == {int} |
| 55 | + assert set(post_process_type(Union[int, SequenceABC[str]])) == {int, str} |
| 56 | + assert set(post_process_type(Union[int, SequenceABC[int]])) == {int} |
| 57 | + |
| 58 | + # Nested Union with lists |
| 59 | + assert set(post_process_type(Union[list[int], list[str]])) == {int, str} |
| 60 | + assert set(post_process_type(Union[int, list[str], list[float]])) == {int, str, float} |
| 61 | + |
| 62 | + # Custom data types |
| 63 | + assert set(post_process_type(Data)) == {Data} |
| 64 | + assert set(post_process_type(list[Data])) == {Data} |
| 65 | + |
| 66 | + # Union with custom types |
| 67 | + assert set(post_process_type(Union[Data, str])) == {Data, str} |
| 68 | + assert set(post_process_type(Union[Data, int, list[str]])) == {Data, int, str} |
| 69 | + |
| 70 | + # Empty lists and edge cases |
| 71 | + assert set(post_process_type(list)) == {list} |
| 72 | + assert set(post_process_type(Union[int, None])) == {int, NoneType} |
| 73 | + assert set(post_process_type(Union[None, list[None]])) == {None, NoneType} |
| 74 | + |
| 75 | + # Handling complex nested structures |
| 76 | + assert set(post_process_type(Union[SequenceABC[Union[int, str]], list[float]])) == {int, str, float} |
| 77 | + assert set(post_process_type(Union[Union[Union[int, list[str]], list[float]], str])) == {int, str, float} |
| 78 | + |
| 79 | + # Non-generic types should return as is |
| 80 | + assert set(post_process_type(dict)) == {dict} |
| 81 | + assert set(post_process_type(tuple)) == {tuple} |
| 82 | + |
| 83 | + # Union with custom types |
| 84 | + assert set(post_process_type(Union[Data, str])) == {Data, str} |
| 85 | + assert set(post_process_type(Data | str)) == {Data, str} |
| 86 | + assert set(post_process_type(Data | int | list[str])) == {Data, int, str} |
| 87 | + |
| 88 | + # More complex combinations with Data |
| 89 | + assert set(post_process_type(Data | list[float])) == {Data, float} |
| 90 | + assert set(post_process_type(Data | Union[int, str])) == {Data, int, str} |
| 91 | + assert set(post_process_type(Data | list[int] | None)) == {Data, int, type(None)} |
| 92 | + assert set(post_process_type(Data | Union[float, None])) == {Data, float, type(None)} |
| 93 | + |
| 94 | + # Multiple Data types combined |
| 95 | + assert set(post_process_type(Union[Data, Union[str, float]])) == {Data, str, float} |
| 96 | + assert set(post_process_type(Union[Data | float | str, int])) == {Data, int, float, str} |
| 97 | + |
| 98 | + # Testing with nested unions and lists |
| 99 | + assert set(post_process_type(Union[list[Data], list[Union[int, str]]])) == {Data, int, str} |
| 100 | + assert set(post_process_type(Data | list[Union[float, str]])) == {Data, float, str} |
48 | 101 |
|
49 | 102 | def test_input_to_dict(self):
|
50 | 103 | input_obj = Input(field_type="str")
|
|
0 commit comments