11from inspect import isclass
2- from unittest .mock import Mock , MagicMock
2+ from unittest .mock import MagicMock , Mock
33
44try :
55 from types import UnionType # type: ignore[attr-defined, unused-ignore]
1111except ImportError : # pragma: no cover
1212 from typing_extensions import TypeIs
1313
14- from typing import List , Type , Union , Any , get_args , get_origin
14+ from typing import Any , List , Type , Union , get_args , get_origin
1515
1616from denial import InnerNoneType
1717
1818from simtypes .typing import ExpectedType
1919
2020
21- def check (value : Any , type_hint : Type [ExpectedType ], strict : bool = False , lists_are_tuples : bool = False , pass_mocks : bool = True ) -> TypeIs [ExpectedType ]:
22- if type_hint is Any : # type: ignore[comparison-overlap]
21+ def check (value : Any , type_hint : Type [ExpectedType ], strict : bool = False , lists_are_tuples : bool = False , pass_mocks : bool = True ) -> TypeIs [ExpectedType ]: # noqa: C901, PLR0911, PLR0912
22+ if type_hint is Any or ( isinstance ( value , ( Mock , MagicMock )) and pass_mocks ) : # type: ignore[comparison-overlap]
2323 return True
2424
25- elif (isinstance (value , Mock ) or isinstance (value , MagicMock )) and pass_mocks :
26- return True
27-
28- elif type_hint is None :
25+ if type_hint is None :
2926 return value is None
3027
31- elif isinstance (type_hint , InnerNoneType ):
28+ if isinstance (type_hint , InnerNoneType ):
3229 return type_hint == value
3330
3431 origin_type = get_origin (type_hint )
3532
3633 if origin_type is Union or origin_type is UnionType :
3734 return any (check (value , argument , strict = strict , lists_are_tuples = lists_are_tuples ) for argument in get_args (type_hint ))
3835
39- elif origin_type is list and strict :
36+ if origin_type is list and strict :
4037 if not isinstance (value , list ):
4138 return False
4239 arguments = get_args (type_hint )
4340 if not arguments :
4441 return True
4542 return all (check (subvalue , arguments [0 ], strict = strict , lists_are_tuples = lists_are_tuples ) for subvalue in value )
4643
47- elif origin_type is dict and strict :
44+ if origin_type is dict and strict :
4845 if not isinstance (value , dict ):
4946 return False
5047 arguments = get_args (type_hint )
5148 if not arguments :
5249 return True
5350 return all (check (key , arguments [0 ], strict = strict , lists_are_tuples = lists_are_tuples ) and check (subvalue , arguments [1 ], strict = strict , lists_are_tuples = lists_are_tuples ) for key , subvalue in value .items ())
5451
55- elif origin_type is tuple and strict :
52+ if origin_type is tuple and strict :
5653 types_to_check : List [Union [Type [list ], Type [tuple ]]] = [tuple ] if not lists_are_tuples else [tuple , list ] # type: ignore[type-arg]
5754 if all (not isinstance (value , x ) for x in types_to_check ):
5855 return False
@@ -70,14 +67,13 @@ def check(value: Any, type_hint: Type[ExpectedType], strict: bool = False, lists
7067
7168 return all (check (subvalue , expected_subtype , strict = strict , lists_are_tuples = lists_are_tuples ) for subvalue , expected_subtype in zip (value , arguments ))
7269
73- else :
74- if origin_type is not None :
75- return isinstance (value , origin_type )
70+ if origin_type is not None :
71+ return isinstance (value , origin_type )
7672
77- if not isclass (type_hint ):
78- raise ValueError ('Type must be a valid type object.' )
73+ if not isclass (type_hint ):
74+ raise ValueError ('Type must be a valid type object.' )
7975
80- if type_hint is tuple and lists_are_tuples :
81- return isinstance (value , tuple ) or isinstance ( value , list ) # pragma: no cover
76+ if type_hint is tuple and lists_are_tuples :
77+ return isinstance (value , ( tuple , list ) ) # pragma: no cover
8278
83- return isinstance (value , type_hint )
79+ return isinstance (value , type_hint )
0 commit comments