-
Notifications
You must be signed in to change notification settings - Fork 5
/
trycast.py
1266 lines (1041 loc) · 41.6 KB
/
trycast.py
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import builtins
import functools
import importlib
import inspect
import math
import re
import sys
from collections.abc import Callable as CCallable
from collections.abc import Mapping as CMapping
from collections.abc import MutableMapping as CMutableMapping
from collections.abc import MutableSequence as CMutableSequence
from collections.abc import Sequence as CSequence
from inspect import Parameter
from types import ModuleType
from typing import ForwardRef # type: ignore[import-error] # pytype (for ForwardRef)
from typing import _GenericAlias # type: ignore[attr-defined]
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
FrozenSet,
List,
Literal,
Mapping,
MutableMapping,
MutableSequence,
NamedTuple,
NewType,
NoReturn,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)
from typing import _eval_type as eval_type # type: ignore[attr-defined]
from typing import _type_repr as type_repr # type: ignore[attr-defined]
from typing import cast, get_args, get_origin, overload
# GenericAlias
if sys.version_info >= (3, 9):
from types import GenericAlias
else:
class GenericAlias(type): # type: ignore[no-redef]
__origin__: object
__args__: Tuple[object, ...]
def __init__(self, origin: object, args: Tuple[object, ...]) -> None: ...
...
# UnionType
if sys.version_info >= (3, 10):
from types import UnionType # type: ignore[attr-defined]
else:
class UnionType(type): # type: ignore[no-redef]
...
# Never
if sys.version_info >= (3, 11):
from typing import Never
else:
class Never(type): # type: ignore[no-redef]
...
# TypeAliasType
if sys.version_info >= (3, 12):
from typing import TypeAliasType # type: ignore[21] # pyre
else:
class TypeAliasType(type): # type: ignore[no-redef]
__type_params__: Tuple[object, ...]
__value__: object
...
# get_type_hints
try:
# If typing_extensions available,
# understands both typing.* and typing_extensions.* types
from typing_extensions import get_type_hints # type: ignore[attr-defined]
except ImportError:
# If typing_extensions not available
from typing import ( # type: ignore[misc, assignment] # incompatible import
get_type_hints,
)
# TypeGuard
if sys.version_info >= (3, 10):
from typing import TypeGuard # Python 3.10+
else:
try:
from typing_extensions import (
TypeGuard, # type: ignore[not-supported-yet] # pytype
)
except ImportError:
if not TYPE_CHECKING:
class TypeGuard:
def __class_getitem__(cls, key):
return bool
# _is_typed_dict
_typed_dict_meta_list = []
try:
from typing import ( # type: ignore[attr-defined] # isort: skip
_TypedDictMeta as _TypingTypedDictMeta, # type: ignore[reportGeneralTypeIssues] # pyright
)
_typed_dict_meta_list.append(_TypingTypedDictMeta) # type: ignore[16] # pyre
except ImportError:
pass
try:
from typing_extensions import ( # type: ignore[attr-defined] # isort: skip
_TypedDictMeta as _TypingExtensionsTypedDictMeta, # type: ignore[reportGeneralTypeIssues] # pyright
)
_typed_dict_meta_list.append(_TypingExtensionsTypedDictMeta) # type: ignore[16] # pyre
except ImportError:
pass
try:
from mypy_extensions import ( # type: ignore[attr-defined] # isort: skip
_TypedDictMeta as _MypyExtensionsTypedDictMeta, # type: ignore[reportGeneralTypeIssues] # pyright
)
_typed_dict_meta_list.append(_MypyExtensionsTypedDictMeta) # type: ignore[16] # pyre
except ImportError:
pass
_typed_dict_metas = tuple(_typed_dict_meta_list)
def _is_typed_dict(tp: object) -> bool:
return isinstance(tp, _typed_dict_metas)
# _is_newtype
if NewType.__class__.__name__ == "function": # type: ignore[reportGeneralTypeIssues] # pyright
# Python 3.8 - 3.9
def _is_newtype(tp: object) -> bool:
return (
hasattr(tp, "__class__")
and tp.__class__.__name__ == "function"
and hasattr(tp, "__qualname__")
and tp.__qualname__.startswith("NewType.<locals>") # type: ignore[attr-defined]
and hasattr(tp, "__module__")
and tp.__module__ == "typing"
)
elif NewType.__class__.__name__ == "type": # type: ignore[reportGeneralTypeIssues] # pyright
# Python 3.10+
def _is_newtype(tp: object) -> bool:
return isinstance(tp, NewType) # type: ignore[arg-type]
else:
raise AssertionError(
"Do not know how to recognize NewType in this version of Python"
)
# _inspect_signature
if sys.version_info >= (3, 10):
def _inspect_signature(value):
return inspect.signature(
value,
# Don't auto-unwrap decorated functions
follow_wrapped=False,
# Don't need annotation information
eval_str=False,
)
else:
def _inspect_signature(value):
return inspect.signature(
value,
# Don't auto-unwrap decorated functions
follow_wrapped=False,
)
# _type_check
if sys.version_info >= (3, 11):
# NOTE: This function is derived from Python 3.12's typing._type_check
# internal helper function. It is however more concerned with
# rejecting known non-types (true negatives) than it is
# avoiding rejecting actual types (false negatives).
# See discussion at: https://github.com/python/cpython/issues/92601
def _type_check(arg: object, msg: str):
"""Returns the argument if it appears to be a type.
Raises TypeError if the argument is a known non-type.
As a special case, accepts None and returns type(None) instead.
Also wraps strings into ForwardRef instances.
"""
arg = _type_convert(arg, module=None)
# Recognize *common* non-types. (This check is not exhaustive.)
if isinstance(arg, (dict, list, int, tuple)):
raise TypeError(f"{msg} Got {arg!r:.100}.")
return arg
# Python 3.10's typing._type_convert()
def _type_convert(arg, module=None):
"""For converting None to type(None), and strings to ForwardRef."""
if arg is None:
return type(None)
if isinstance(arg, str):
return ForwardRef(arg, module=module)
return arg
else:
from typing import _type_check # type: ignore[attr-defined]
__all__ = (
"trycast",
"checkcast",
"isassignable",
# NOTE: May be part of the API in the future
# "eval_type_str",
)
_T = TypeVar("_T")
_F = TypeVar("_F")
_SimpleTypeVar = TypeVar("_SimpleTypeVar")
_SimpleTypeVarCo = TypeVar("_SimpleTypeVarCo", covariant=True) # type: ignore[not-supported-yet] # pytest
_MISSING = object()
# ------------------------------------------------------------------------------
# trycast
# TODO: Once support for TypeForm is implemented in mypy,
# replace the `(Type[T]) -> Optional[T]` overload
# and the `(object) -> Optional[object]` overload with
# the following `(TypeForm[T]) -> Optional[T]` overload:
#
# See: https://github.com/python/mypy/issues/9773
# @overload
# def trycast(tp: TypeForm[_T], value: object) -> Optional[_T]: ...
# Overload: (tp: str, eval: Literal[False]) -> NoReturn
@overload
def trycast( # type: ignore[43] # pyre
tp: str, value: object, /, *, strict: bool = True, eval: Literal[False]
) -> NoReturn: ... # pragma: no cover
# Overload Group: (tp: str|Type[_T]|object, value: object) -> ...
@overload
def trycast(tp: str, value: object, /, *, strict: bool = True, eval: bool = True) -> bool: # type: ignore[43] # pyre
... # pragma: no cover
@overload
def trycast( # type: ignore[43] # pyre
tp: Type[_T], value: object, /, *, strict: bool = True, eval: bool = True
) -> Optional[_T]: ... # pragma: no cover
@overload
def trycast( # type: ignore[43] # pyre
tp: object, value: object, /, *, strict: bool = True, eval: bool = True
) -> Optional[object]: ... # pragma: no cover
# Overload Group: (tp: str|Type[_T]|object, value: object, failure: object) -> ...
@overload
def trycast(
tp: str,
value: object,
/,
failure: object,
*,
strict: bool = True,
eval: Literal[False],
) -> NoReturn: ... # pragma: no cover
@overload
def trycast(
tp: Type[_T],
value: object,
/,
failure: _F,
*,
strict: bool = True,
eval: bool = True,
) -> Union[_T, _F]: ... # pragma: no cover
@overload
def trycast(
tp: object, value: object, /, failure: _F, *, strict: bool = True, eval: bool = True
) -> Union[object, _F]: ... # pragma: no cover
# Implementation
def trycast(tp, value, /, failure=None, *, strict=True, eval=True):
"""
If `value` is in the shape of `tp` (as accepted by a Python typechecker
conforming to PEP 484 "Type Hints") then returns it, otherwise returns
`failure` (which is None by default).
This method logically performs an operation similar to:
return value if isinstance(tp, value) else failure
except that it supports many more types than `isinstance`, including:
* List[T]
* Dict[K, V]
* Optional[T]
* Union[T1, T2, ...]
* Literal[...]
* T extends TypedDict
Similar to isinstance(), this method considers every bool value to
also be a valid int value, as consistent with Python typecheckers:
> trycast(int, True) -> True
> isinstance(True, int) -> True
Note that unlike isinstance(), this method considers every int value to
also be a valid float or complex value, as consistent with Python typecheckers:
> trycast(float, 1) -> 1
> trycast(complex, 1) -> 1
> isinstance(1, float) -> False
> isinstance(1, complex) -> False
Note that unlike isinstance(), this method considers every float value to
also be a valid complex value, as consistent with Python typecheckers:
> trycast(complex, 1.0) -> 1
> isinstance(1.0, complex) -> False
Parameters:
* strict --
* If strict=False then this function will additionally accept
mypy_extensions.TypedDict instances and Python 3.8 typing.TypedDict
instances for the `tp` parameter. Normally these kinds of types are
rejected with a TypeNotSupportedError because these
types do not preserve enough information at runtime to reliably
determine which keys are required and which are potentially-missing.
* If strict=False then `NewType("Foo", T)` will be treated
the same as `T`. Normally NewTypes are rejected with a
TypeNotSupportedError because values of NewTypes at runtime
are indistinguishable from their wrapped supertype.
* eval --
If eval=False then this function will not attempt to resolve string
type references, which requires the use of the eval() function.
Otherwise string type references will be accepted.
Raises:
* TypeNotSupportedError --
* If strict=True and either mypy_extensions.TypedDict or a
Python 3.8 typing.TypedDict is found within the `tp` argument.
* If strict=True and a NewType is found within the `tp` argument.
* If a TypeVar is found within the `tp` argument.
* If an unrecognized Generic type is found within the `tp` argument.
* UnresolvedForwardRefError --
If `tp` is a type form which contains a ForwardRef.
* UnresolvableTypeError --
If `tp` is a string that could not be resolved to a type.
"""
e = _checkcast_outer(tp, value, _TrycastOptions(strict, eval, funcname="trycast"))
if e is not None:
return failure # type: ignore[bad-return-type] # pytype
else:
return value # type: ignore[bad-return-type] # pytype
# ------------------------------------------------------------------------------
# checkcast
# TODO: Once support for TypeForm is implemented in mypy,
# replace the `(Type[T]) -> Optional[T]` overload
# and the `(object) -> Optional[object]` overload with
# the following `(TypeForm[T]) -> Optional[T]` overload:
#
# See: https://github.com/python/mypy/issues/9773
# @overload
# def checkcast(tp: TypeForm[_T], value: object) -> Optional[_T]: ...
# Overload: (tp: str, eval: Literal[False]) -> NoReturn
@overload
def checkcast( # type: ignore[43] # pyre
tp: str,
value: object,
/,
*,
strict: bool = True,
eval: Literal[False],
_funcname: str = "checkcast",
) -> NoReturn: ... # pragma: no cover
# Overload Group: (tp: str|Type[_T]|object, value: object) -> ...
@overload
def checkcast(tp: str, value: object, /, *, strict: bool = True, eval: bool = True, _funcname: str = "checkcast") -> bool: # type: ignore[43] # pyre
... # pragma: no cover
@overload
def checkcast( # type: ignore[43] # pyre
tp: Type[_T],
value: object,
/,
*,
strict: bool = True,
eval: bool = True,
_funcname: str = "checkcast",
) -> Optional[_T]: ... # pragma: no cover
@overload
def checkcast( # type: ignore[43] # pyre
tp: object,
value: object,
/,
*,
strict: bool = True,
eval: bool = True,
_funcname: str = "checkcast",
) -> Optional[object]: ... # pragma: no cover
# Implementation
def checkcast(tp, value, /, *, strict=True, eval=True, _funcname="checkcast"):
"""
If `value` is in the shape of `tp` (as accepted by a Python typechecker
conforming to PEP 484 "Type Hints") then returns it, otherwise
raises ValidationError
This method logically performs an operation similar to:
if isinstance(tp, value):
return value
else:
raise ValidationError(tp, value)
except that it supports many more types than `isinstance`, including:
* List[T]
* Dict[K, V]
* Optional[T]
* Union[T1, T2, ...]
* Literal[...]
* T extends TypedDict
See trycast.trycast() for information about parameters,
raised exceptions, and other details.
Raises:
* ValidationError -- If `value` is not in the shape of `tp`.
* TypeNotSupportedError
* UnresolvedForwardRefError
* UnresolvableTypeError
"""
e = _checkcast_outer(tp, value, _TrycastOptions(strict, eval, _funcname))
if e is not None:
raise e
else:
return value # type: ignore[bad-return-type] # pytype
class _TrycastOptions(NamedTuple):
strict: bool
eval: bool
funcname: str
def _checkcast_outer(
tp: object, value: object, options: _TrycastOptions
) -> "Optional[ValidationError]":
if isinstance(tp, str):
if options.eval: # == options.eval (for pytype)
tp = eval_type_str(tp) # does use eval()
else:
raise UnresolvableTypeError(
f"Could not resolve type {tp!r}: "
f"Type appears to be a string reference "
f"and {options.funcname}() was called with eval=False, "
f"disabling eval of string type references."
)
else:
try:
# TODO: Eliminate format operation done by f-string
# from the hot path of _checkcast_outer()
tp = _type_check( # type: ignore[16] # pyre
tp,
f"{options.funcname}() requires a type as its first argument.",
)
except TypeError:
if isinstance(tp, tuple) and len(tp) >= 1 and isinstance(tp[0], type):
raise TypeError(
f"{options.funcname} does not support checking against a tuple of types. "
"Try checking against a Union[T1, T2, ...] instead."
)
else:
raise
try:
return _checkcast_inner(tp, value, options) # type: ignore[bad-return-type] # pytype
except UnresolvedForwardRefError:
if options.eval:
advise = (
"Try altering the first type argument to be a string "
"reference (surrounded with quotes) instead."
)
else:
advise = (
f"{options.funcname}() cannot resolve string type references "
"because it was called with eval=False."
)
raise UnresolvedForwardRefError(
f"{options.funcname} does not support checking against type form {tp!r} "
"which contains a string-based forward reference. "
f"{advise}"
)
def _checkcast_inner(
tp: object, value: object, options: _TrycastOptions
) -> "Optional[ValidationError]":
"""
Raises:
* TypeNotSupportedError
* UnresolvedForwardRefError
"""
if tp is int:
# Also accept bools as valid int values
if isinstance(value, int):
return None
else:
return ValidationError(tp, value)
if tp is float:
# Also accept ints and bools as valid float values
if isinstance(value, float) or isinstance(value, int):
return None
else:
return ValidationError(tp, value)
if tp is complex:
# Also accept floats, ints, and bools as valid complex values
if (
isinstance(value, complex)
or isinstance(value, float)
or isinstance(value, int)
):
return None
else:
return ValidationError(tp, value)
type_origin = get_origin(tp)
if type_origin is list or type_origin is List: # List, List[T]
return _checkcast_listlike(tp, value, list, options)
if type_origin is set or type_origin is Set: # Set, Set[T]
return _checkcast_listlike(tp, value, set, options)
if type_origin is frozenset or type_origin is FrozenSet: # FrozenSet, FrozenSet[T]
return _checkcast_listlike(tp, value, frozenset, options, covariant_t=True)
if type_origin is tuple or type_origin is Tuple:
if isinstance(value, tuple):
type_args = get_args(tp)
if len(type_args) == 0 or (
len(type_args) == 2 and type_args[1] is Ellipsis
): # Tuple, Tuple[T, ...]
return _checkcast_listlike(
tp,
value,
tuple,
options,
covariant_t=True,
t_ellipsis=True,
)
else: # Tuple[Ts]
if len(value) != len(type_args):
return ValidationError(tp, value)
for i, T, t in zip(range(len(type_args)), type_args, value):
e = _checkcast_inner(T, t, options)
if e is not None:
return ValidationError(
tp,
value,
_causes=[e._with_prefix(_LazyStr(lambda: f"At index {i}"))],
)
return None
else:
return ValidationError(tp, value)
if type_origin is Sequence or type_origin is CSequence: # Sequence, Sequence[T]
return _checkcast_listlike(tp, value, CSequence, options, covariant_t=True)
if (
type_origin is MutableSequence or type_origin is CMutableSequence
): # MutableSequence, MutableSequence[T]
return _checkcast_listlike(tp, value, CMutableSequence, options)
if type_origin is dict or type_origin is Dict: # Dict, Dict[K, V]
return _checkcast_dictlike(tp, value, dict, options)
if type_origin is Mapping or type_origin is CMapping: # Mapping, Mapping[K, V]
return _checkcast_dictlike(tp, value, CMapping, options, covariant_v=True)
if (
type_origin is MutableMapping or type_origin is CMutableMapping
): # MutableMapping, MutableMapping[K, V]
return _checkcast_dictlike(tp, value, CMutableMapping, options)
if (
type_origin is Union or type_origin is UnionType
): # Union[T1, T2, ...], Optional[T]
causes = []
for T in get_args(tp):
e = _checkcast_inner(T, value, options)
if e is not None:
causes.append(e)
else:
return None
return ValidationError(tp, value, _causes=causes)
if type_origin is Literal: # Literal[...]
for literal in get_args(tp):
if value == literal:
return None
return ValidationError(tp, value)
if type_origin is CCallable:
callable_args = get_args(tp)
if callable_args == ():
# Callable
if callable(value):
return None
else:
return ValidationError(tp, value)
else:
assert len(callable_args) == 2
(param_types, return_type) = callable_args
if return_type is not Any:
# Callable[..., T]
raise TypeNotSupportedError(
f"{options.funcname} cannot reliably determine whether value is "
f"a {type_repr(tp)} because "
f"callables at runtime do not always have a "
f"declared return type. "
f"Consider using {options.funcname}(Callable, value) instead."
)
if param_types is Ellipsis:
# Callable[..., Any]
return _checkcast_inner(Callable, value, options)
assert isinstance(param_types, list)
for param_type in param_types:
if param_type is not Any:
raise TypeNotSupportedError(
f"{options.funcname} cannot reliably determine whether value is "
f"a {type_repr(tp)} because "
f"callables at runtime do not always have "
f"declared parameter types. "
f"Consider using {options.funcname}("
f"Callable[{','.join('Any' * len(param_types))}, Any], value) "
f"instead."
)
# Callable[[Any * N], Any]
if callable(value):
try:
sig = _inspect_signature(value)
except TypeError:
# Not a callable
return ValidationError(tp, value)
except ValueError as f:
# Unable to introspect signature for value.
# It might be a built-in function that lacks signature support.
# Assume conservatively that value does NOT match the requested type.
e = ValidationError(tp, value)
e.__cause__ = f
return e
else:
sig_min_param_count = 0 # type: float
sig_max_param_count = 0 # type: float
for expected_param in sig.parameters.values():
if (
expected_param.kind == Parameter.POSITIONAL_ONLY
or expected_param.kind == Parameter.POSITIONAL_OR_KEYWORD
):
if expected_param.default is Parameter.empty:
sig_min_param_count += 1
sig_max_param_count += 1
elif expected_param.kind == Parameter.VAR_POSITIONAL:
sig_max_param_count = math.inf
if sig_min_param_count <= len(param_types) <= sig_max_param_count:
return None
else:
return ValidationError(tp, value)
else:
return ValidationError(tp, value)
if isinstance(type_origin, TypeAliasType): # type: ignore[16] # pyre
if len(type_origin.__type_params__) > 0:
substitutions = dict(
zip(
type_origin.__type_params__,
get_args(tp) + ((Any,) * len(type_origin.__type_params__)),
)
) # type: Dict[object, object]
new_tp = _substitute(tp.__value__, substitutions) # type: ignore[attr-defined] # mypy
else:
new_tp = tp.__value__ # type: ignore[attr-defined] # mypy
return _checkcast_inner(new_tp, value, options) # type: ignore[16] # pyre
if isinstance(tp, _GenericAlias): # type: ignore[16] # pyre
raise TypeNotSupportedError(
f"{options.funcname} does not know how to recognize generic type "
f"{type_repr(type_origin)}."
)
if _is_typed_dict(tp): # T extends TypedDict
if isinstance(value, Mapping):
if options.eval:
resolved_annotations = get_type_hints( # does use eval()
tp # type: ignore[arg-type] # mypy
) # resolve ForwardRefs in tp.__annotations__
else:
resolved_annotations = tp.__annotations__ # type: ignore[attribute-error] # pytype
try:
# {typing in Python 3.9+, typing_extensions}.TypedDict
required_keys = tp.__required_keys__ # type: ignore[attr-defined, attribute-error] # mypy, pytype
except AttributeError:
# {typing in Python 3.8, mypy_extensions}.TypedDict
if options.strict:
if sys.version_info[:2] >= (3, 9):
advise = "Suggest use a typing.TypedDict instead."
else:
advise = "Suggest use a typing_extensions.TypedDict instead."
advise2 = f"Or use {options.funcname}(..., strict=False)."
raise TypeNotSupportedError(
f"{options.funcname} cannot determine which keys are required "
f"and which are potentially-missing for the "
f"specified kind of TypedDict. {advise} {advise2}"
)
else:
if tp.__total__: # type: ignore[attr-defined, attribute-error] # mypy, pytype
required_keys = resolved_annotations.keys()
else:
required_keys = frozenset()
for k, v in value.items(): # type: ignore[attribute-error] # pytype
V = resolved_annotations.get(k, _MISSING)
if V is not _MISSING:
e = _checkcast_inner(V, v, options)
if e is not None:
return ValidationError(
tp,
value,
_causes=[e._with_prefix(_LazyStr(lambda: f"At key {k!r}"))],
)
for k in required_keys:
if k not in value: # type: ignore[unsupported-operands] # pytype
return ValidationError(
tp,
value,
_causes=[
ValidationError._from_message(
_LazyStr(lambda: f"Required key {k!r} is missing")
)
],
)
return None
else:
return ValidationError(tp, value)
if _is_newtype(tp):
if options.strict:
supertype_repr = type_repr(tp.__supertype__) # type: ignore[attr-defined, attribute-error] # mypy, pytype
tp_name_repr = repr(tp.__name__) # type: ignore[attr-defined] # mypy
raise TypeNotSupportedError(
f"{options.funcname} cannot reliably determine whether value is "
f"a NewType({tp_name_repr}, {supertype_repr}) because "
f"NewType wrappers are erased at runtime "
f"and are indistinguishable from their supertype. "
f"Consider using {options.funcname}(..., strict=False) to treat "
f"NewType({tp_name_repr}, {supertype_repr}) "
f"like {supertype_repr}."
)
else:
supertype = tp.__supertype__ # type: ignore[attr-defined, attribute-error] # mypy, pytype
return _checkcast_inner(supertype, value, options)
if isinstance(tp, TypeVar): # type: ignore[wrong-arg-types] # pytype
raise TypeNotSupportedError(
f"{options.funcname} cannot reliably determine whether value matches a TypeVar."
)
if tp is Any:
return None
if tp is Never or tp is NoReturn:
return ValidationError(tp, value)
if isinstance(tp, TypeAliasType): # type: ignore[16] # pyre
if len(tp.__type_params__) > 0: # type: ignore[16] # pyre
substitutions = dict(
zip(tp.__type_params__, ((Any,) * len(tp.__type_params__)))
)
new_tp = _substitute(tp.__value__, substitutions)
else:
new_tp = tp.__value__
return _checkcast_inner(new_tp, value, options) # type: ignore[16] # pyre
if isinstance(tp, ForwardRef):
raise UnresolvedForwardRefError()
if isinstance(value, tp): # type: ignore[arg-type, wrong-arg-types] # mypy, pytype
return None
else:
return ValidationError(tp, value)
class TypeNotSupportedError(TypeError):
pass
class UnresolvedForwardRefError(TypeError):
pass
def _substitute(tp: object, substitutions: Dict[object, object]) -> object:
if isinstance(tp, GenericAlias): # ex: tuple[T1, T2]
return GenericAlias( # type: ignore[reportCallIssue] # pyright
tp.__origin__, tuple([_substitute(a, substitutions) for a in tp.__args__])
)
if isinstance(tp, TypeVar): # type: ignore[wrong-arg-types] # pytype
return substitutions.get(tp, tp)
return tp
def _checkcast_listlike(
tp: object,
value: object,
listlike_type: Type,
options: _TrycastOptions,
*,
covariant_t: bool = False,
t_ellipsis: bool = False,
) -> "Optional[ValidationError]":
if isinstance(value, listlike_type):
T_ = get_args(tp)
if len(T_) == 0: # Python 3.9+
(T,) = (_SimpleTypeVarCo if covariant_t else _SimpleTypeVar,)
else:
if t_ellipsis:
if len(T_) == 2 and T_[1] is Ellipsis:
(T, _) = T_
else:
return ValidationError(tp, value)
else:
(T,) = T_
if _is_simple_typevar(T, covariant=covariant_t):
pass
else:
for i, x in enumerate(value): # type: ignore[attribute-error] # pytype
e = _checkcast_inner(T, x, options)
if e is not None:
return ValidationError(
tp,
value,
_causes=[e._with_prefix(_LazyStr(lambda: f"At index {i}"))],
)
return None
else:
return ValidationError(tp, value)
def _checkcast_dictlike(
tp: object,
value: object,
dictlike_type: Type,
options: _TrycastOptions,
*,
covariant_v: bool = False,
) -> "Optional[ValidationError]":
if isinstance(value, dictlike_type):
K_V = get_args(tp)
if len(K_V) == 0: # Python 3.9+
(K, V) = (
_SimpleTypeVar,
_SimpleTypeVarCo if covariant_v else _SimpleTypeVar,
)
else:
(K, V) = K_V
if _is_simple_typevar(K) and _is_simple_typevar(V, covariant=covariant_v):
pass
else:
for k, v in value.items(): # type: ignore[attribute-error] # pytype
e = _checkcast_inner(K, k, options)
if e is not None:
return ValidationError(
tp,
value,
_causes=[e._with_prefix(_LazyStr(lambda: f"Key {k!r}"))],
)
e = _checkcast_inner(V, v, options)
if e is not None:
return ValidationError(
tp,
value,
_causes=[e._with_prefix(_LazyStr(lambda: f"At key {k!r}"))],
)
return None
else:
return ValidationError(tp, value)
def _is_simple_typevar(T: object, covariant: bool = False) -> bool:
return (
isinstance(T, TypeVar) # type: ignore[wrong-arg-types] # pytype
and T.__constraints__ == () # type: ignore[attribute-error] # pytype
and T.__covariant__ == covariant # type: ignore[attribute-error] # pytype
and T.__contravariant__ is False # type: ignore[attribute-error] # pytype
and T.__constraints__ == () # type: ignore[attribute-error] # pytype
)
# ------------------------------------------------------------------------------
# ValidationError
if sys.version_info >= (3, 11):
from typing import Self as _SelfValidationError
else:
_SelfValidationError = TypeVar("_SelfValidationError", bound="ValidationError")
class ValidationError(ValueError):
# === Init ===
def __init__(
self,
tp: object,
value: object,
/,
# NOTE: Inner type and structure for representing "cause" information
# is private and may change in the future.
_causes: "Optional[Sequence[ValidationError]]" = None,
*,
_message: "Optional[_LazyStr]" = None,
) -> None:
"""
Creates a ValidationError related to the specified value not matching
the expected specified type.
Parameters (positional-only):
* tp -- the expected type of the specified value.