1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
from dataclasses import InitVar
from sys import version_info
from typing import Optional, Union, List, Any, Dict, NewType, TypeVar, Generic, Collection, Tuple, Type
from unittest.mock import patch, Mock
import pytest
from dacite.types import (
is_optional,
extract_optional,
is_generic,
is_union,
is_generic_collection,
extract_origin_collection,
is_instance,
extract_generic,
is_new_type,
extract_new_type,
is_literal,
is_init_var,
extract_init_var,
is_type_generic,
is_tuple,
)
from tests.common import (
literal_support,
init_var_type_support,
pep_604_support,
type_hints_with_generic_collections_support,
)
def test_is_union_with_union():
assert is_union(Union[int, float])
def test_is_union_with_non_union():
assert not is_union(int)
def test_is_union_with_import_error():
with patch("builtins.__import__") as mock_import:
mock_import.side_effect = ImportError()
assert not is_union(str)
def test_is_tuple_with_tuple():
assert is_tuple(Tuple[int, float, str])
def test_is_tuple_with_variable_length_tuple():
assert is_tuple(Tuple[int, ...])
def test_is_tuple_with_not_parametrized_tuple():
assert is_tuple(Tuple)
def test_is_tuple_with_tuple_class_object():
assert is_tuple(tuple)
@type_hints_with_generic_collections_support
def test_is_tuple_with_tuple_generic():
assert is_tuple(tuple[int, float, str])
@type_hints_with_generic_collections_support
def test_is_tuple_with_variable_length_tuple_generic():
assert is_tuple(tuple[int, ...])
def test_is_tuple_with_non_tuple():
assert not is_tuple(int)
@pep_604_support
def test_is_union_with_pep_604_union():
assert is_union(int | float)
@literal_support
def test_is_literal_with_literal():
from typing import Literal
assert is_literal(Literal["A", "B"])
def test_is_literal_with_non_literal():
assert not is_literal(int)
def test_is_literal_with_import_error():
with patch("builtins.__import__") as mock_import:
mock_import.side_effect = ImportError()
assert not is_literal(str)
def test_is_init_var_with_init_var():
assert is_init_var(InitVar[int])
def test_is_init_var_with_non_init_var():
assert not is_init_var(int)
def test_is_optional_with_optional():
assert is_optional(Optional[int])
def test_is_optional_with_non_optional():
assert not is_optional(int)
def test_is_optional_with_optional_of_union():
assert is_optional(Optional[Union[int, float]])
@pep_604_support
def test_is_optional_with_pep_604_union():
assert is_optional(int | float | None)
@pep_604_support
def test_is_optional_with_non_optional_pep_604_union():
assert not is_optional(int | float)
def test_extract_optional():
assert extract_optional(Optional[int]) == int
def test_extract_optional_with_wrong_type():
with pytest.raises(ValueError):
extract_optional(List[None])
def test_extract_optional_with_optional_of_union():
assert extract_optional(Optional[Union[int, str]]) == Union[int, str]
def test_is_generic_with_generic():
assert is_generic(Optional[int])
def test_is_generic_with_non_generic():
assert not is_generic(int)
def test_is_generic_collection_with_generic_collection():
assert is_generic_collection(List[int])
def test_is_generic_collection_with_non_generic_collection():
assert not is_generic_collection(list)
def test_is_generic_collection_with_union():
assert not is_generic_collection(Union[int, str])
def test_extract_generic_collection():
assert extract_origin_collection(List[int]) == list
def test_is_new_type_with_new_type():
assert is_new_type(NewType("NewType", int))
def test_is_new_type_with_non_new_type():
assert not is_new_type(int)
def test_extract_new_type():
assert extract_new_type(NewType("NewType", int)) == int
def test_is_instance_with_built_in_type_and_matching_value_type():
assert is_instance(1, int)
def test_is_instance_with_built_in_type_and_not_matching_value_type():
assert not is_instance("test", int)
def test_is_instance_with_union_and_matching_value_type():
assert is_instance(1, Union[int, float])
def test_is_instance_with_union_and_not_matching_value_type():
assert not is_instance("test", Union[int, float])
def test_is_instance_with_union_and_matching_generic_collection():
assert is_instance(["test"], Union[int, List[str]])
def test_is_instance_with_union_and_not_matching_generic_collection():
assert not is_instance(["test"], Union[int, List[int]])
def test_is_instance_with_optional_and_matching_value_type():
assert is_instance(1, Optional[int])
def test_is_instance_with_optional_and_not_matching_value_type():
assert not is_instance(1, Optional[str])
def test_is_instance_with_generic_collection_and_matching_value_type():
assert is_instance([1], List[int])
def test_is_instance_with_generic_collection_and_not_matching_item_type():
assert not is_instance(["test"], List[int])
def test_is_instance_with_nested_generic_collection_and_matching_value_type():
assert is_instance([[1]], List[List[int]])
def test_is_instance_with_nested_generic_collection_and_not_matching_item_type():
assert not is_instance([["test"]], List[List[int]])
def test_is_instance_with_generic_collection_without_specified_inner_types_and_matching_value_type():
assert is_instance([1], List)
def test_is_instance_with_generic_collection_without_specified_inner_types_and_not_matching_value_type():
assert not is_instance([1], Dict)
def test_is_instance_with_generic_abstract_collection_and_matching_value_type():
assert is_instance([1], Collection[int])
def test_is_instance_with_generic_collection_and_not_matching_value_type():
assert not is_instance({1}, List[int])
def test_is_instance_with_any_type():
assert is_instance(1, Any)
def test_is_instance_with_new_type_and_matching_value_type():
assert is_instance("test", NewType("MyStr", str))
def test_is_instance_with_new_type_and_not_matching_value_type():
assert not is_instance(1, NewType("MyStr", str))
@init_var_type_support
def test_is_instance_with_init_var_and_matching_value_type():
assert is_instance(1, InitVar[int])
@init_var_type_support
def test_is_instance_with_init_var_and_not_matching_value_type():
assert not is_instance(1, InitVar[str])
def test_is_instance_with_with_type_and_matching_value_type():
assert is_instance(str, Type[str])
def test_is_instance_with_with_type_and_not_matching_value_type():
assert not is_instance(1, Type[str])
def test_is_instance_with_not_supported_generic_types():
T = TypeVar("T")
class X(Generic[T]):
pass
assert not is_instance(X[str](), X[str])
def test_is_instance_with_generic_mapping_and_matching_value_type():
assert is_instance({"test": 1}, Dict[str, int])
def test_is_instance_with_generic_mapping_and_not_matching_mapping_key_type():
assert not is_instance({1: 1}, Dict[str, int])
def test_is_instance_with_generic_mapping_and_not_matching_mapping_value_type():
assert not is_instance({"test": "test"}, Dict[str, int])
def test_is_instance_with_numeric_tower():
assert is_instance(1, float)
def test_is_instance_with_numeric_tower_and_optional():
assert is_instance(1, Optional[float])
def test_is_instance_with_numeric_tower_and_new_type():
assert is_instance(1, NewType("NewType", float))
@literal_support
def test_is_instance_with_literal_and_matching_type():
from typing import Literal
assert is_instance("A", Literal["A", "B"])
@literal_support
def test_is_instance_with_literal_and_not_matching_type():
from typing import Literal
assert not is_instance("C", Literal["A", "B"])
@literal_support
def test_is_instance_with_optional_literal_and_matching_type():
from typing import Literal
assert is_instance("A", Optional[Literal["A", "B"]])
@literal_support
def test_is_instance_with_optional_literal_and_not_matching_type():
from typing import Literal
assert not is_instance("C", Optional[Literal["A", "B"]])
@literal_support
def test_is_instance_with_optional_literal_and_none():
from typing import Literal
assert is_instance(None, Optional[Literal["A", "B"]])
def test_is_instance_with_tuple_and_matching_type():
assert is_instance((1, "test"), Tuple[int, str])
def test_is_instance_with_tuple_and_not_matching_type():
assert not is_instance((1, 2), Tuple[int, str])
def test_is_instance_with_tuple_and_wrong_length():
assert not is_instance((1, "test", 2), Tuple[int, str])
def test_is_instance_with_variable_length_tuple_and_matching_type():
assert is_instance((1, 2, 3), Tuple[int, ...])
def test_is_instance_with_variable_length_tuple_and_not_matching_type():
assert not is_instance((1, 2, "test"), Tuple[int, ...])
def test_is_instance_with_empty_tuple_and_matching_type():
assert is_instance((), Tuple[()])
def test_is_instance_with_empty_tuple_and_not_matching_type():
assert not is_instance((1, 2), Tuple[()])
def test_extract_generic():
assert extract_generic(List[int]) == (int,)
def test_extract_generic_with_defaults():
assert extract_generic(List, defaults=(Any,)) == (Any,)
@init_var_type_support
def test_extract_init_var():
assert extract_init_var(InitVar[int]) == int
def test_extract_init_var_with_attribute_error():
class FakeType:
pass
assert extract_init_var(FakeType) == Any
def test_is_type_generic_with_matching_value():
assert is_type_generic(Type[int])
def test_is_type_generic_with_not_matching_value():
assert not is_type_generic(int)
def test_extract_generic_special():
defaults = 1, 2
class FakeType:
_special = True
assert extract_generic(FakeType, defaults) == defaults
@pytest.mark.skipif(version_info < (3, 10), reason="writing union types as X | Y requires Python 3.10")
def test_optional_and_union_none_does_not_pollute_scope_via_caching():
is_generic(Optional[str])
@pep_604_support
def test_optional_and_union_none_does_not_pollute_scope_via_caching_pep_604():
is_generic_collection(str | None)
|