Coverage for tests / unit / validate_type / test_validate_type.py: 79%

223 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 21:32 -0600

1from __future__ import annotations 

2 

3import typing 

4from typing import Any, Optional, Union 

5 

6import pytest 

7 

8from muutils.validate_type import IncorrectTypeException, validate_type 

9 

10 

11# Tests for basic types and common use cases 

12@pytest.mark.parametrize( 

13 "value, expected_type, expected_result", 

14 [ 

15 (42, int, True), 

16 (3.14, float, True), 

17 (5, int, True), 

18 (5.0, int, False), 

19 ("hello", str, True), 

20 (True, bool, True), 

21 (None, type(None), True), 

22 (None, int, False), 

23 ([1, 2, 3], typing.List, True), 

24 ([1, 2, 3], typing.List, True), 

25 ({"a": 1, "b": 2}, typing.Dict, True), 

26 ({"a": 1, "b": 2}, typing.Dict, True), 

27 ({1, 2, 3}, typing.Set, True), 

28 ({1, 2, 3}, typing.Set, True), 

29 ((1, 2, 3), typing.Tuple, True), 

30 ((1, 2, 3), typing.Tuple, True), 

31 (b"bytes", bytes, True), 

32 (b"bytes", str, False), 

33 ("3.14", float, False), 

34 ("hello", Any, True), 

35 (5, Any, True), 

36 (3.14, Any, True), 

37 # ints 

38 (int(0), int, True), 

39 (int(1), int, True), 

40 (int(-1), int, True), 

41 # bools 

42 (True, bool, True), 

43 (False, bool, True), 

44 ], 

45) 

46def test_validate_type_basic(value, expected_type, expected_result): 

47 try: 

48 assert validate_type(value, expected_type) == expected_result 

49 except Exception as e: 

50 raise Exception( 

51 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

52 ) from e 

53 

54 

55@pytest.mark.parametrize( 

56 "value", 

57 [ 

58 42, 

59 "hello", 

60 3.14, 

61 True, 

62 None, 

63 [1, 2, 3], 

64 {"a": 1, "b": 2}, 

65 {1, 2, 3}, 

66 (1, 2, 3), 

67 b"bytes", 

68 "3.14", 

69 ], 

70) 

71def test_validate_type_any(value): 

72 try: 

73 assert validate_type(value, Any) 

74 except Exception as e: 

75 raise Exception(f"{value = }, expected `Any`, {e}") from e 

76 

77 

78@pytest.mark.parametrize( 

79 "value, expected_type, expected_result", 

80 [ 

81 (42, Union[int, str], True), 

82 ("hello", Union[int, str], True), 

83 (3.14, Union[int, float], True), 

84 (True, Union[int, str], True), 

85 (None, Union[int, None], True), 

86 (None, Union[int, str], False), 

87 (5, Union[int, str], True), 

88 (5.0, Union[int, str], False), 

89 ("hello", Union[int, str], True), 

90 (5, typing.Union[int, str], True), 

91 ("hello", typing.Union[int, str], True), 

92 (5.0, typing.Union[int, str], False), 

93 (5, Union[int, str], True), 

94 ("hello", Union[int, str], True), 

95 (5.0, Union[int, str], False), 

96 ], 

97) 

98def test_validate_type_union(value, expected_type, expected_result): 

99 try: 

100 assert validate_type(value, expected_type) == expected_result 

101 except Exception as e: 

102 raise Exception( 

103 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

104 ) from e 

105 

106 

107@pytest.mark.parametrize( 

108 "value, expected_type, expected_result", 

109 [ 

110 (42, Optional[int], True), 

111 ("hello", Optional[int], False), 

112 (3.14, Optional[int], False), 

113 ([1], Optional[typing.List[int]], True), 

114 (None, Optional[int], True), 

115 (None, Optional[str], True), 

116 (None, Optional[int], True), 

117 (None, Optional[None], True), 

118 (None, Optional[typing.List[typing.Dict[str, int]]], True), 

119 ], 

120) 

121def test_validate_type_optional(value, expected_type, expected_result): 

122 try: 

123 assert validate_type(value, expected_type) == expected_result 

124 except Exception as e: 

125 raise Exception( 

126 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

127 ) from e 

128 

129 

130@pytest.mark.parametrize( 

131 "value, expected_type, expected_result", 

132 [ 

133 (42, typing.List[int], False), 

134 ([1, 2, 3], typing.List[int], True), 

135 ([1, 2, 3], typing.List[str], False), 

136 (["a", "b", "c"], typing.List[str], True), 

137 ([1, "a", 3], typing.List[int], False), 

138 (42, typing.List[int], False), 

139 ([1, 2, 3], typing.List[int], True), 

140 ([1, "2", 3], typing.List[int], False), 

141 ], 

142) 

143def test_validate_type_list(value, expected_type, expected_result): 

144 try: 

145 assert validate_type(value, expected_type) == expected_result 

146 except Exception as e: 

147 raise Exception( 

148 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

149 ) from e 

150 

151 

152@pytest.mark.parametrize( 

153 "value, expected_type, expected_result", 

154 [ 

155 (42, typing.Dict[str, int], False), 

156 ({"a": 1, "b": 2}, typing.Dict[str, int], True), 

157 ({"a": 1, "b": 2}, typing.Dict[int, str], False), 

158 (42, typing.Dict[str, int], False), 

159 ({"a": 1, "b": 2}, typing.Dict[str, int], True), 

160 ({"a": 1, "b": 2}, typing.Dict[int, str], False), 

161 ({1: "a", 2: "b"}, typing.Dict[int, str], True), 

162 ({1: "a", 2: "b"}, typing.Dict[str, int], False), 

163 ({"a": 1, "b": "c"}, typing.Dict[str, int], False), 

164 ([("a", 1), ("b", 2)], typing.Dict[str, int], False), 

165 ({"key": "value"}, typing.Dict[str, str], True), 

166 ({"key": 2}, typing.Dict[str, str], False), 

167 ({"key": 2}, typing.Dict[str, int], True), 

168 ({"key": 2.0}, typing.Dict[str, int], False), 

169 ({"a": 1, "b": 2}, typing.Dict[str, int], True), 

170 ({"a": 1, "b": "2"}, typing.Dict[str, int], False), 

171 ], 

172) 

173def test_validate_type_dict(value, expected_type, expected_result): 

174 try: 

175 assert validate_type(value, expected_type) == expected_result 

176 except Exception as e: 

177 raise Exception( 

178 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

179 ) from e 

180 

181 

182@pytest.mark.parametrize( 

183 "value, expected_type, expected_result", 

184 [ 

185 (42, typing.Set[int], False), 

186 ({1, 2, 3}, typing.Set[int], True), 

187 (42, typing.Set[int], False), 

188 ({1, 2, 3}, typing.Set[int], True), 

189 ({1, 2, 3}, typing.Set[str], False), 

190 ({"a", "b", "c"}, typing.Set[str], True), 

191 ({1, "a", 3}, typing.Set[int], False), 

192 (42, typing.Set[int], False), 

193 ({1, 2, 3}, typing.Set[int], True), 

194 ({1, "2", 3}, typing.Set[int], False), 

195 ([1, 2, 3], typing.Set[int], False), 

196 ("hello", typing.Set[str], False), 

197 ], 

198) 

199def test_validate_type_set(value, expected_type, expected_result): 

200 try: 

201 assert validate_type(value, expected_type) == expected_result 

202 except Exception as e: 

203 raise Exception( 

204 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

205 ) from e 

206 

207 

208@pytest.mark.parametrize( 

209 "value, expected_type, expected_result", 

210 [ 

211 (42, typing.Tuple[int, str], False), 

212 ((1, "a"), typing.Tuple[int, str], True), 

213 (42, typing.Tuple[int, str], False), 

214 ((1, "a"), typing.Tuple[int, str], True), 

215 ((1, 2), typing.Tuple[int, str], False), 

216 ((1, 2), typing.Tuple[int, int], True), 

217 ((1, 2, 3), typing.Tuple[int, int], False), 

218 ((1, "a", 3.14), typing.Tuple[int, str, float], True), 

219 (("a", "b", "c"), typing.Tuple[str, str, str], True), 

220 ((1, "a", 3.14), typing.Tuple[int, str], False), 

221 ((1, "a", 3.14), typing.Tuple[int, str, float], True), 

222 ([1, "a", 3.14], typing.Tuple[int, str, float], False), 

223 ( 

224 (1, "a", 3.14, "b", True, None, (1, 2, 3)), 

225 # no idea why this throws type error, only locally, and only for the generated modern types 

226 typing.Tuple[ # type: ignore[misc] 

227 int, str, float, str, bool, None, typing.Tuple[int, int, int] 

228 ], 

229 True, 

230 ), 

231 ], 

232) 

233def test_validate_type_tuple(value, expected_type, expected_result): 

234 try: 

235 assert validate_type(value, expected_type) == expected_result 

236 except Exception as e: 

237 raise Exception( 

238 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

239 ) from e 

240 

241 

242@pytest.mark.parametrize( 

243 "value, expected_type, expected_result", 

244 [ 

245 # basic string literals 

246 ("a", typing.Literal["a", "b"], True), 

247 ("b", typing.Literal["a", "b"], True), 

248 ("c", typing.Literal["a", "b"], False), 

249 # basic int literals 

250 (1, typing.Literal[1, 2, 3], True), 

251 (4, typing.Literal[1, 2, 3], False), 

252 # single-value literal 

253 ("a", typing.Literal["a"], True), 

254 ("b", typing.Literal["a"], False), 

255 # mixed-type literal 

256 ("a", typing.Literal["a", 1], True), 

257 (1, typing.Literal["a", 1], True), 

258 (2, typing.Literal["a", 1], False), 

259 # None in literal 

260 (None, typing.Literal[None, "x"], True), 

261 ("x", typing.Literal[None, "x"], True), 

262 (1, typing.Literal[None, "x"], False), 

263 # bool literal (note: True == 1 in Python, so True in Literal[True] is True) 

264 (True, typing.Literal[True], True), 

265 (False, typing.Literal[True], False), 

266 # Literal nested in Optional 

267 (None, typing.Optional[typing.Literal["a", "b"]], True), 

268 ("a", typing.Optional[typing.Literal["a", "b"]], True), 

269 ("c", typing.Optional[typing.Literal["a", "b"]], False), 

270 # Literal nested in Union 

271 ("a", typing.Union[typing.Literal["a", "b"], int], True), 

272 (1, typing.Union[typing.Literal["a", "b"], int], True), 

273 ("c", typing.Union[typing.Literal["a", "b"], int], False), 

274 # List[Literal[...]] 

275 (["a", "b"], typing.List[typing.Literal["a", "b", "c"]], True), 

276 (["a", "d"], typing.List[typing.Literal["a", "b", "c"]], False), 

277 ([], typing.List[typing.Literal["a", "b"]], True), 

278 # Dict[str, Literal[...]] 

279 ({"k": "a"}, typing.Dict[str, typing.Literal["a", "b"]], True), 

280 ({"k": "c"}, typing.Dict[str, typing.Literal["a", "b"]], False), 

281 ({}, typing.Dict[str, typing.Literal["a", "b"]], True), 

282 # Dict[Literal[...], int] 

283 ({"x": 1, "y": 2}, typing.Dict[typing.Literal["x", "y"], int], True), 

284 ({"x": 1, "z": 2}, typing.Dict[typing.Literal["x", "y"], int], False), 

285 ({}, typing.Dict[typing.Literal["x", "y"], int], True), 

286 # Set[Literal[...]] 

287 ({"a", "b"}, typing.Set[typing.Literal["a", "b", "c"]], True), 

288 ({"a", "d"}, typing.Set[typing.Literal["a", "b", "c"]], False), 

289 (set(), typing.Set[typing.Literal["a", "b"]], True), 

290 # Tuple[Literal[...], Literal[...]] 

291 ( 

292 ("fast", 1), 

293 typing.Tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]], 

294 True, 

295 ), 

296 ( 

297 ("fast", 3), 

298 typing.Tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]], 

299 False, 

300 ), 

301 ( 

302 ("bad", 1), 

303 typing.Tuple[typing.Literal["fast", "slow"], typing.Literal[1, 2]], 

304 False, 

305 ), 

306 # bool/int Literal ambiguity: mirrors Python's bool-is-int semantics (True == 1, False == 0) 

307 (True, typing.Literal[1], True), 

308 (False, typing.Literal[0], True), 

309 (1, typing.Literal[True], True), 

310 (0, typing.Literal[False], True), 

311 ], 

312) 

313def test_validate_type_literal(value, expected_type, expected_result): 

314 try: 

315 assert validate_type(value, expected_type) == expected_result 

316 except Exception as e: 

317 raise Exception( 

318 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

319 ) from e 

320 

321 

322def test_validate_type_literal_do_except(): 

323 assert validate_type("a", typing.Literal["a", "b"], do_except=True) 

324 with pytest.raises(IncorrectTypeException): 

325 validate_type("c", typing.Literal["a", "b"], do_except=True) 

326 

327 

328def test_validate_type_list_do_except(): 

329 # valid list — no raise 

330 assert validate_type([1, 2, 3], typing.List[int], do_except=True) 

331 assert validate_type( 

332 ["a", "b"], typing.List[typing.Literal["a", "b"]], do_except=True 

333 ) 

334 # bad element — must raise, not return False 

335 with pytest.raises(IncorrectTypeException): 

336 validate_type([1, "bad"], typing.List[int], do_except=True) 

337 with pytest.raises(IncorrectTypeException): 

338 validate_type( 

339 ["a", "bad"], typing.List[typing.Literal["a", "b"]], do_except=True 

340 ) 

341 

342 

343@pytest.mark.parametrize( 

344 "value, expected_type", 

345 [ 

346 (43, typing.Callable), 

347 (lambda x: x, typing.Callable), 

348 (42, typing.Callable[[], None]), 

349 (42, typing.Callable[[int, str], typing.List]), 

350 ], 

351) 

352def test_validate_type_unsupported_type_hint(value, expected_type): 

353 with pytest.raises(NotImplementedError): 

354 validate_type(value, expected_type) 

355 print(f"Failed to except: {value = }, {expected_type = }") 

356 

357 

358@pytest.mark.parametrize( 

359 "value, expected_type, expected_result", 

360 [ 

361 ([1, 2, 3], typing.List[int], True), 

362 (["a", "b", "c"], typing.List[str], True), 

363 ([1, "a", 3], typing.List[int], False), 

364 ([1, 2, [3, 4]], typing.List[Union[int, typing.List[int]]], True), 

365 ([(1, 2), (3, 4)], typing.List[typing.Tuple[int, int]], True), 

366 ([(1, 2), (3, "4")], typing.List[typing.Tuple[int, int]], False), 

367 ({1: [1, 2], 2: [3, 4]}, typing.Dict[int, typing.List[int]], True), 

368 ({1: [1, 2], 2: [3, "4"]}, typing.Dict[int, typing.List[int]], False), 

369 ], 

370) 

371def test_validate_type_collections(value, expected_type, expected_result): 

372 try: 

373 assert validate_type(value, expected_type) == expected_result 

374 except Exception as e: 

375 raise Exception( 

376 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

377 ) from e 

378 

379 

380@pytest.mark.parametrize( 

381 "value, expected_type, expected_result", 

382 [ 

383 # empty lists 

384 ([], typing.List[int], True), 

385 ([], typing.List[typing.Dict], True), 

386 ( 

387 [], 

388 typing.List[typing.Tuple[typing.Dict[typing.Tuple, str], str, None]], 

389 True, 

390 ), 

391 # empty dicts 

392 ({}, typing.Dict[str, int], True), 

393 ({}, typing.Dict[str, typing.Dict], True), 

394 ({}, typing.Dict[str, typing.Dict[str, int]], True), 

395 ({}, typing.Dict[str, typing.Dict[str, int]], True), 

396 # empty sets 

397 (set(), typing.Set[int], True), 

398 (set(), typing.Set[typing.Dict], True), 

399 ( 

400 set(), 

401 typing.Set[typing.Tuple[typing.Dict[typing.Tuple, str], str, None]], 

402 True, 

403 ), 

404 # empty tuple 

405 (tuple(), typing.Tuple, True), 

406 # empty string 

407 ("", str, True), 

408 # empty bytes 

409 (b"", bytes, True), 

410 # None 

411 (None, type(None), True), 

412 # bools are ints, ints are not floats 

413 (True, int, True), 

414 (False, int, True), 

415 (True, float, False), 

416 (False, float, False), 

417 (1, int, True), 

418 (0, int, True), 

419 (1, float, False), 

420 (0, float, False), 

421 (0, bool, False), 

422 (1, bool, False), 

423 # weird floats 

424 (float("nan"), float, True), 

425 (float("inf"), float, True), 

426 (float("-inf"), float, True), 

427 (float(0), float, True), 

428 # list/tuple 

429 ([1], typing.Tuple[int, int], False), 

430 ((1, 2), typing.List[int], False), 

431 ], 

432) 

433def test_validate_type_edge_cases(value, expected_type, expected_result): 

434 try: 

435 assert validate_type(value, expected_type) == expected_result 

436 except Exception as e: 

437 raise Exception( 

438 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

439 ) from e 

440 

441 

442@pytest.mark.parametrize( 

443 "value, expected_type, expected_result", 

444 [ 

445 (42, typing.List[int], False), 

446 ([1, 2, 3], int, False), 

447 (3.14, typing.Tuple[float], False), 

448 (3.14, typing.Tuple[float, float], False), 

449 (3.14, typing.Tuple[bool, str], False), 

450 (False, typing.Tuple[bool, str], False), 

451 (False, typing.Tuple[bool], False), 

452 ((False,), typing.Tuple[bool], True), 

453 (("abc",), typing.Tuple[str], True), 

454 ("test-dict", typing.Dict[str, int], False), 

455 ("test-dict", typing.Dict, False), 

456 ], 

457) 

458def test_validate_type_wrong_type(value, expected_type, expected_result): 

459 try: 

460 assert validate_type(value, expected_type) == expected_result 

461 except Exception as e: 

462 raise Exception( 

463 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

464 ) from e 

465 

466 

467def test_validate_type_complex(): 

468 assert validate_type([1, 2, [3, 4]], typing.List[Union[int, typing.List[int]]]) 

469 assert validate_type( 

470 {"a": 1, "b": {"c": 2}}, typing.Dict[str, Union[int, typing.Dict[str, int]]] 

471 ) 

472 assert validate_type({1, (2, 3)}, typing.Set[Union[int, typing.Tuple[int, int]]]) 

473 assert validate_type((1, ("a", "b")), typing.Tuple[int, typing.Tuple[str, str]]) 

474 assert validate_type([{"key": "value"}], typing.List[typing.Dict[str, str]]) 

475 assert validate_type([{"key": 2}], typing.List[typing.Dict[str, str]]) is False 

476 assert validate_type([[1, 2], [3, 4]], typing.List[typing.List[int]]) 

477 assert validate_type([[1, 2], [3, "4"]], typing.List[typing.List[int]]) is False 

478 assert validate_type([(1, 2), (3, 4)], typing.List[typing.Tuple[int, int]]) 

479 assert ( 

480 validate_type([(1, 2), (3, "4")], typing.List[typing.Tuple[int, int]]) is False 

481 ) 

482 assert validate_type({1: "one", 2: "two"}, typing.Dict[int, str]) 

483 assert validate_type({1: "one", 2: 2}, typing.Dict[int, str]) is False 

484 assert validate_type([(1, "one"), (2, "two")], typing.List[typing.Tuple[int, str]]) 

485 assert ( 

486 validate_type([(1, "one"), (2, 2)], typing.List[typing.Tuple[int, str]]) 

487 is False 

488 ) 

489 assert validate_type({1: [1, 2], 2: [3, 4]}, typing.Dict[int, typing.List[int]]) 

490 assert ( 

491 validate_type({1: [1, 2], 2: [3, "4"]}, typing.Dict[int, typing.List[int]]) 

492 is False 

493 ) 

494 assert validate_type([(1, "a"), (2, "b")], typing.List[typing.Tuple[int, str]]) 

495 assert ( 

496 validate_type([(1, "a"), (2, 2)], typing.List[typing.Tuple[int, str]]) is False 

497 ) 

498 

499 

500@pytest.mark.parametrize( 

501 "value, expected_type, expected_result", 

502 [ 

503 ([[[[1]]]], typing.List[typing.List[typing.List[typing.List[int]]]], True), 

504 ([[[[1]]]], typing.List[typing.List[typing.List[typing.List[str]]]], False), 

505 ( 

506 {"a": {"b": {"c": 1}}}, 

507 typing.Dict[str, typing.Dict[str, typing.Dict[str, int]]], 

508 True, 

509 ), 

510 ( 

511 {"a": {"b": {"c": 1}}}, 

512 typing.Dict[str, typing.Dict[str, typing.Dict[str, str]]], 

513 False, 

514 ), 

515 ({1, 2, 3}, typing.Set[int], True), 

516 ({1, 2, 3}, typing.Set[str], False), 

517 ( 

518 ((1, 2), (3, 4)), 

519 typing.Tuple[typing.Tuple[int, int], typing.Tuple[int, int]], 

520 True, 

521 ), 

522 ( 

523 ((1, 2), (3, 4)), 

524 typing.Tuple[typing.Tuple[int, int], typing.Tuple[int, str]], 

525 False, 

526 ), 

527 ], 

528) 

529def test_validate_type_nested(value, expected_type, expected_result): 

530 try: 

531 assert validate_type(value, expected_type) == expected_result 

532 except Exception as e: 

533 raise Exception( 

534 f"{value = }, {expected_type = }, {expected_result = }, {e}" 

535 ) from e 

536 

537 

538def test_validate_type_inheritance(): 

539 class Parent: 

540 def __init__(self, a: int, b: str): 

541 self.a: int = a 

542 self.b: str = b 

543 

544 class Child(Parent): 

545 def __init__(self, a: int, b: str): 

546 self.a: int = 2 * a 

547 self.b: str = b 

548 

549 assert validate_type(Parent(1, "a"), Parent) 

550 validate_type(Child(1, "a"), Parent, do_except=True) 

551 assert validate_type(Child(1, "a"), Child) 

552 assert not validate_type(Parent(1, "a"), Child) 

553 

554 with pytest.raises(IncorrectTypeException): 

555 validate_type(Parent(1, "a"), Child, do_except=True) 

556 

557 

558def test_validate_type_class(): 

559 class Parent: 

560 def __init__(self, a: int, b: str): 

561 self.a: int = a 

562 self.b: str = b 

563 

564 class Child(Parent): 

565 def __init__(self, a: int, b: str): 

566 self.a: int = 2 * a 

567 self.b: str = b 

568 

569 assert validate_type(Parent, type) 

570 assert validate_type(Child, type) 

571 assert validate_type(Parent, typing.Type[Parent], do_except=True) 

572 assert validate_type(Child, typing.Type[Child]) 

573 assert not validate_type(Parent, typing.Type[Child]) 

574 

575 assert validate_type(Child, typing.Union[typing.Type[Child], typing.Type[Parent]]) 

576 assert validate_type(Child, typing.Union[typing.Type[Child], int]) 

577 

578 

579@pytest.mark.skip(reason="Not implemented") 

580def test_validate_type_class_union(): 

581 class Parent: 

582 def __init__(self, a: int, b: str): 

583 self.a: int = a 

584 self.b: str = b 

585 

586 class Child(Parent): 

587 def __init__(self, a: int, b: str): 

588 self.a: int = 2 * a 

589 self.b: str = b 

590 

591 class Other: 

592 def __init__(self, x: int, y: str): 

593 self.x: int = x 

594 self.y: str = y 

595 

596 assert validate_type(Child, typing.Type[typing.Union[Child, Parent]]) 

597 assert validate_type(Child, typing.Type[typing.Union[Child, Other]]) 

598 assert validate_type(Parent, typing.Type[typing.Union[Child, Other]]) 

599 assert validate_type(Parent, typing.Type[typing.Union[Parent, Other]]) 

600 

601 

602def test_validate_type_aliases(): 

603 AliasInt = int 

604 AliasStr = str 

605 AliasListInt = typing.List[int] 

606 AliasListStr = typing.List[str] 

607 AliasDictIntStr = typing.Dict[int, str] 

608 AliasDictStrInt = typing.Dict[str, int] 

609 AliasTupleIntStr = typing.Tuple[int, str] 

610 AliasTupleStrInt = typing.Tuple[str, int] 

611 AliasSetInt = typing.Set[int] 

612 AliasSetStr = typing.Set[str] 

613 AliasUnionIntStr = typing.Union[int, str] 

614 AliasUnionStrInt = typing.Union[str, int] 

615 AliasOptionalInt = typing.Optional[int] 

616 AliasOptionalStr = typing.Optional[str] 

617 AliasOptionalListInt = typing.Optional[typing.List[int]] 

618 AliasDictStrListInt = typing.Dict[str, typing.List[int]] 

619 

620 assert validate_type(42, AliasInt) 

621 assert not validate_type("42", AliasInt) 

622 assert validate_type(42, AliasInt) 

623 assert not validate_type("42", AliasInt) 

624 assert validate_type("hello", AliasStr) 

625 assert not validate_type(42, AliasStr) 

626 assert validate_type([1, 2, 3], AliasListInt) 

627 assert not validate_type([1, "2", 3], AliasListInt) 

628 assert validate_type(["hello", "world"], AliasListStr) 

629 assert not validate_type(["hello", 42], AliasListStr) 

630 assert validate_type({1: "a", 2: "b"}, AliasDictIntStr) 

631 assert not validate_type({1: 2, 3: 4}, AliasDictIntStr) 

632 assert validate_type({"one": 1, "two": 2}, AliasDictStrInt) 

633 assert not validate_type({1: "one", 2: "two"}, AliasDictStrInt) 

634 assert validate_type((1, "a"), AliasTupleIntStr) 

635 assert not validate_type(("a", 1), AliasTupleIntStr) 

636 assert validate_type(("a", 1), AliasTupleStrInt) 

637 assert not validate_type((1, "a"), AliasTupleStrInt) 

638 assert validate_type({1, 2, 3}, AliasSetInt) 

639 assert not validate_type({1, "two", 3}, AliasSetInt) 

640 assert validate_type({"one", "two"}, AliasSetStr) 

641 assert not validate_type({"one", 2}, AliasSetStr) 

642 assert validate_type(42, AliasUnionIntStr) 

643 assert validate_type("hello", AliasUnionIntStr) 

644 assert not validate_type(3.14, AliasUnionIntStr) 

645 assert validate_type("hello", AliasUnionStrInt) 

646 assert validate_type(42, AliasUnionStrInt) 

647 assert not validate_type(3.14, AliasUnionStrInt) 

648 assert validate_type(42, AliasOptionalInt) 

649 assert validate_type(None, AliasOptionalInt) 

650 assert not validate_type("42", AliasOptionalInt) 

651 assert validate_type("hello", AliasOptionalStr) 

652 assert validate_type(None, AliasOptionalStr) 

653 assert not validate_type(42, AliasOptionalStr) 

654 assert validate_type([1, 2, 3], AliasOptionalListInt) 

655 assert validate_type(None, AliasOptionalListInt) 

656 assert not validate_type(["1", "2", "3"], AliasOptionalListInt) 

657 assert validate_type({"key": [1, 2, 3]}, AliasDictStrListInt) 

658 assert not validate_type({"key": [1, "2", 3]}, AliasDictStrListInt)