| 1 |
import typing |
| 2 |
from sys import version_info |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from ninja.signature.details import is_collection_type |
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.parametrize( |
| 10 |
("annotation", "expected"), |
| 11 |
[ |
| 12 |
pytest.param(typing.List, True, id="true_for_typing_List"), |
| 13 |
pytest.param(list, True, id="true_for_native_list"), |
| 14 |
pytest.param(typing.Set, True, id="true_for_typing_Set"), |
| 15 |
pytest.param(set, True, id="true_for_native_set"), |
| 16 |
pytest.param(typing.Tuple, True, id="true_for_typing_Tuple"), |
| 17 |
pytest.param(tuple, True, id="true_for_native_tuple"), |
| 18 |
pytest.param( |
| 19 |
typing.Optional[typing.List[str]], True, id="true_for_optional_list" |
| 20 |
), |
| 21 |
pytest.param( |
| 22 |
type("Custom", (), {}), |
| 23 |
False, |
| 24 |
id="false_for_custom_type_without_typing_origin", |
| 25 |
), |
| 26 |
pytest.param( |
| 27 |
object(), False, id="false_for_custom_instance_without_typing_origin" |
| 28 |
), |
| 29 |
pytest.param( |
| 30 |
typing.NewType("SomethingNew", str), |
| 31 |
False, |
| 32 |
id="false_for_instance_without_typing_origin", |
| 33 |
), |
| 34 |
|
| 35 |
|
| 36 |
*( |
| 37 |
( |
| 38 |
pytest.param(list[int], True, id="true_for_parameterized_native_list"), |
| 39 |
pytest.param(set[int], True, id="true_for_parameterized_native_set"), |
| 40 |
pytest.param( |
| 41 |
tuple[int], True, id="true_for_parameterized_native_tuple" |
| 42 |
), |
| 43 |
) |
| 44 |
|
| 45 |
if version_info >= (3, 9) |
| 46 |
else () |
| 47 |
), |
| 48 |
], |
| 49 |
) |
| 50 |
def test_is_collection_type_returns(annotation: typing.Any, expected: bool): |
| 51 |
assert is_collection_type(annotation) is expected |