Coverage for bioimageio/spec/_internal/type_guards.py: 86%

21 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-02 14:21 +0000

1import collections.abc 

2from typing import Any, Dict, List, Mapping, Sequence, Set, Tuple 

3 

4import numpy as np 

5from numpy.typing import NDArray 

6from typing_extensions import TypeGuard 

7 

8 

9def is_dict(v: Any) -> TypeGuard[Dict[Any, Any]]: 

10 """to avoid Dict[Unknown, Unknown]""" 

11 return isinstance(v, dict) 

12 

13 

14def is_set(v: Any) -> TypeGuard[Set[Any]]: 

15 """to avoid Set[Unknown]""" 

16 return isinstance(v, set) 

17 

18 

19def is_kwargs(v: Any) -> TypeGuard[Dict[str, Any]]: 

20 return isinstance(v, dict) and all( 

21 isinstance(k, str) for k in v # pyright: ignore[reportUnknownVariableType] 

22 ) 

23 

24 

25def is_mapping(v: Any) -> TypeGuard[Mapping[Any, Any]]: 

26 """to avoid Mapping[Unknown, Unknown]""" 

27 return isinstance(v, collections.abc.Mapping) 

28 

29 

30def is_sequence(v: Any) -> TypeGuard[Sequence[Any]]: 

31 """to avoid Sequence[Unknown]""" 

32 return isinstance(v, collections.abc.Sequence) 

33 

34 

35def is_tuple(v: Any) -> TypeGuard[Tuple[Any, ...]]: 

36 """to avoid Tuple[Unknown, ...]""" 

37 return isinstance(v, tuple) 

38 

39 

40def is_list(v: Any) -> TypeGuard[List[Any]]: 

41 """to avoid List[Unknown]""" 

42 return isinstance(v, list) 

43 

44 

45def is_ndarray(v: Any) -> TypeGuard[NDArray[Any]]: 

46 return isinstance(v, np.ndarray)