Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/numpy/_typing/_array_like.py: 95%

42 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-09 06:12 +0000

1from __future__ import annotations 

2 

3import sys 

4from collections.abc import Collection, Callable, Sequence 

5from typing import Any, Protocol, Union, TypeVar, runtime_checkable 

6 

7import numpy as np 

8from numpy import ( 

9 ndarray, 

10 dtype, 

11 generic, 

12 unsignedinteger, 

13 integer, 

14 floating, 

15 complexfloating, 

16 number, 

17 timedelta64, 

18 datetime64, 

19 object_, 

20 void, 

21 str_, 

22 bytes_, 

23) 

24from ._nested_sequence import _NestedSequence 

25 

26_T = TypeVar("_T") 

27_ScalarType = TypeVar("_ScalarType", bound=generic) 

28_ScalarType_co = TypeVar("_ScalarType_co", bound=generic, covariant=True) 

29_DType = TypeVar("_DType", bound=dtype[Any]) 

30_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any]) 

31 

32NDArray = ndarray[Any, dtype[_ScalarType_co]] 

33 

34# The `_SupportsArray` protocol only cares about the default dtype 

35# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned 

36# array. 

37# Concrete implementations of the protocol are responsible for adding 

38# any and all remaining overloads 

39@runtime_checkable 

40class _SupportsArray(Protocol[_DType_co]): 

41 def __array__(self) -> ndarray[Any, _DType_co]: ... 

42 

43 

44@runtime_checkable 

45class _SupportsArrayFunc(Protocol): 

46 """A protocol class representing `~class.__array_function__`.""" 

47 def __array_function__( 

48 self, 

49 func: Callable[..., Any], 

50 types: Collection[type[Any]], 

51 args: tuple[Any, ...], 

52 kwargs: dict[str, Any], 

53 ) -> object: ... 

54 

55 

56# TODO: Wait until mypy supports recursive objects in combination with typevars 

57_FiniteNestedSequence = Union[ 

58 _T, 

59 Sequence[_T], 

60 Sequence[Sequence[_T]], 

61 Sequence[Sequence[Sequence[_T]]], 

62 Sequence[Sequence[Sequence[Sequence[_T]]]], 

63] 

64 

65# A subset of `npt.ArrayLike` that can be parametrized w.r.t. `np.generic` 

66_ArrayLike = Union[ 

67 _SupportsArray[dtype[_ScalarType]], 

68 _NestedSequence[_SupportsArray[dtype[_ScalarType]]], 

69] 

70 

71# A union representing array-like objects; consists of two typevars: 

72# One representing types that can be parametrized w.r.t. `np.dtype` 

73# and another one for the rest 

74_DualArrayLike = Union[ 

75 _SupportsArray[_DType], 

76 _NestedSequence[_SupportsArray[_DType]], 

77 _T, 

78 _NestedSequence[_T], 

79] 

80 

81if sys.version_info >= (3, 12): 

82 from collections.abc import Buffer 

83 

84 ArrayLike = Buffer | _DualArrayLike[ 

85 dtype[Any], 

86 Union[bool, int, float, complex, str, bytes], 

87 ] 

88else: 

89 ArrayLike = _DualArrayLike[ 

90 dtype[Any], 

91 Union[bool, int, float, complex, str, bytes], 

92 ] 

93 

94# `ArrayLike<X>_co`: array-like objects that can be coerced into `X` 

95# given the casting rules `same_kind` 

96_ArrayLikeBool_co = _DualArrayLike[ 

97 dtype[np.bool], 

98 bool, 

99] 

100_ArrayLikeUInt_co = _DualArrayLike[ 

101 dtype[Union[np.bool, unsignedinteger[Any]]], 

102 bool, 

103] 

104_ArrayLikeInt_co = _DualArrayLike[ 

105 dtype[Union[np.bool, integer[Any]]], 

106 Union[bool, int], 

107] 

108_ArrayLikeFloat_co = _DualArrayLike[ 

109 dtype[Union[np.bool, integer[Any], floating[Any]]], 

110 Union[bool, int, float], 

111] 

112_ArrayLikeComplex_co = _DualArrayLike[ 

113 dtype[Union[ 

114 np.bool, 

115 integer[Any], 

116 floating[Any], 

117 complexfloating[Any, Any], 

118 ]], 

119 Union[bool, int, float, complex], 

120] 

121_ArrayLikeNumber_co = _DualArrayLike[ 

122 dtype[Union[np.bool, number[Any]]], 

123 Union[bool, int, float, complex], 

124] 

125_ArrayLikeTD64_co = _DualArrayLike[ 

126 dtype[Union[np.bool, integer[Any], timedelta64]], 

127 Union[bool, int], 

128] 

129_ArrayLikeDT64_co = Union[ 

130 _SupportsArray[dtype[datetime64]], 

131 _NestedSequence[_SupportsArray[dtype[datetime64]]], 

132] 

133_ArrayLikeObject_co = Union[ 

134 _SupportsArray[dtype[object_]], 

135 _NestedSequence[_SupportsArray[dtype[object_]]], 

136] 

137 

138_ArrayLikeVoid_co = Union[ 

139 _SupportsArray[dtype[void]], 

140 _NestedSequence[_SupportsArray[dtype[void]]], 

141] 

142_ArrayLikeStr_co = _DualArrayLike[ 

143 dtype[str_], 

144 str, 

145] 

146_ArrayLikeBytes_co = _DualArrayLike[ 

147 dtype[bytes_], 

148 bytes, 

149] 

150 

151_ArrayLikeInt = _DualArrayLike[ 

152 dtype[integer[Any]], 

153 int, 

154] 

155 

156# Extra ArrayLike type so that pyright can deal with NDArray[Any] 

157# Used as the first overload, should only match NDArray[Any], 

158# not any actual types. 

159# https://github.com/numpy/numpy/pull/22193 

160class _UnknownType: 

161 ... 

162 

163 

164_ArrayLikeUnknown = _DualArrayLike[ 

165 dtype[_UnknownType], 

166 _UnknownType, 

167]