m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
12 KiB

6 months ago
  1. """
  2. A module with various ``typing.Protocol`` subclasses that implement
  3. the ``__call__`` magic method.
  4. See the `Mypy documentation`_ on protocols for more details.
  5. .. _`Mypy documentation`: https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols
  6. """
  7. from __future__ import annotations
  8. import sys
  9. from typing import (
  10. Union,
  11. TypeVar,
  12. overload,
  13. Any,
  14. Tuple,
  15. NoReturn,
  16. TYPE_CHECKING,
  17. )
  18. from numpy import (
  19. ndarray,
  20. dtype,
  21. generic,
  22. bool_,
  23. timedelta64,
  24. number,
  25. integer,
  26. unsignedinteger,
  27. signedinteger,
  28. int8,
  29. int_,
  30. floating,
  31. float64,
  32. complexfloating,
  33. complex128,
  34. )
  35. from ._nbit import _NBitInt, _NBitDouble
  36. from ._scalars import (
  37. _BoolLike_co,
  38. _IntLike_co,
  39. _FloatLike_co,
  40. _ComplexLike_co,
  41. _NumberLike_co,
  42. )
  43. from . import NBitBase
  44. from ._array_like import ArrayLike
  45. from ._generic_alias import NDArray
  46. if sys.version_info >= (3, 8):
  47. from typing import Protocol
  48. HAVE_PROTOCOL = True
  49. else:
  50. try:
  51. from typing_extensions import Protocol
  52. except ImportError:
  53. HAVE_PROTOCOL = False
  54. else:
  55. HAVE_PROTOCOL = True
  56. if TYPE_CHECKING or HAVE_PROTOCOL:
  57. _T1 = TypeVar("_T1")
  58. _T2 = TypeVar("_T2")
  59. _2Tuple = Tuple[_T1, _T1]
  60. _NBit1 = TypeVar("_NBit1", bound=NBitBase)
  61. _NBit2 = TypeVar("_NBit2", bound=NBitBase)
  62. _IntType = TypeVar("_IntType", bound=integer)
  63. _FloatType = TypeVar("_FloatType", bound=floating)
  64. _NumberType = TypeVar("_NumberType", bound=number)
  65. _NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number)
  66. _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic)
  67. class _BoolOp(Protocol[_GenericType_co]):
  68. @overload
  69. def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ...
  70. @overload # platform dependent
  71. def __call__(self, __other: int) -> int_: ...
  72. @overload
  73. def __call__(self, __other: float) -> float64: ...
  74. @overload
  75. def __call__(self, __other: complex) -> complex128: ...
  76. @overload
  77. def __call__(self, __other: _NumberType) -> _NumberType: ...
  78. class _BoolBitOp(Protocol[_GenericType_co]):
  79. @overload
  80. def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ...
  81. @overload # platform dependent
  82. def __call__(self, __other: int) -> int_: ...
  83. @overload
  84. def __call__(self, __other: _IntType) -> _IntType: ...
  85. class _BoolSub(Protocol):
  86. # Note that `__other: bool_` is absent here
  87. @overload
  88. def __call__(self, __other: bool) -> NoReturn: ...
  89. @overload # platform dependent
  90. def __call__(self, __other: int) -> int_: ...
  91. @overload
  92. def __call__(self, __other: float) -> float64: ...
  93. @overload
  94. def __call__(self, __other: complex) -> complex128: ...
  95. @overload
  96. def __call__(self, __other: _NumberType) -> _NumberType: ...
  97. class _BoolTrueDiv(Protocol):
  98. @overload
  99. def __call__(self, __other: Union[float, _IntLike_co]) -> float64: ...
  100. @overload
  101. def __call__(self, __other: complex) -> complex128: ...
  102. @overload
  103. def __call__(self, __other: _NumberType) -> _NumberType: ...
  104. class _BoolMod(Protocol):
  105. @overload
  106. def __call__(self, __other: _BoolLike_co) -> int8: ...
  107. @overload # platform dependent
  108. def __call__(self, __other: int) -> int_: ...
  109. @overload
  110. def __call__(self, __other: float) -> float64: ...
  111. @overload
  112. def __call__(self, __other: _IntType) -> _IntType: ...
  113. @overload
  114. def __call__(self, __other: _FloatType) -> _FloatType: ...
  115. class _BoolDivMod(Protocol):
  116. @overload
  117. def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ...
  118. @overload # platform dependent
  119. def __call__(self, __other: int) -> _2Tuple[int_]: ...
  120. @overload
  121. def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
  122. @overload
  123. def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ...
  124. @overload
  125. def __call__(self, __other: _FloatType) -> _2Tuple[_FloatType]: ...
  126. class _TD64Div(Protocol[_NumberType_co]):
  127. @overload
  128. def __call__(self, __other: timedelta64) -> _NumberType_co: ...
  129. @overload
  130. def __call__(self, __other: _BoolLike_co) -> NoReturn: ...
  131. @overload
  132. def __call__(self, __other: _FloatLike_co) -> timedelta64: ...
  133. class _IntTrueDiv(Protocol[_NBit1]):
  134. @overload
  135. def __call__(self, __other: bool) -> floating[_NBit1]: ...
  136. @overload
  137. def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
  138. @overload
  139. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  140. @overload
  141. def __call__(
  142. self, __other: complex
  143. ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
  144. @overload
  145. def __call__(self, __other: integer[_NBit2]) -> floating[Union[_NBit1, _NBit2]]: ...
  146. class _UnsignedIntOp(Protocol[_NBit1]):
  147. # NOTE: `uint64 + signedinteger -> float64`
  148. @overload
  149. def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
  150. @overload
  151. def __call__(
  152. self, __other: Union[int, signedinteger[Any]]
  153. ) -> Any: ...
  154. @overload
  155. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  156. @overload
  157. def __call__(
  158. self, __other: complex
  159. ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
  160. @overload
  161. def __call__(
  162. self, __other: unsignedinteger[_NBit2]
  163. ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
  164. class _UnsignedIntBitOp(Protocol[_NBit1]):
  165. @overload
  166. def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
  167. @overload
  168. def __call__(self, __other: int) -> signedinteger[Any]: ...
  169. @overload
  170. def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ...
  171. @overload
  172. def __call__(
  173. self, __other: unsignedinteger[_NBit2]
  174. ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
  175. class _UnsignedIntMod(Protocol[_NBit1]):
  176. @overload
  177. def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
  178. @overload
  179. def __call__(
  180. self, __other: Union[int, signedinteger[Any]]
  181. ) -> Any: ...
  182. @overload
  183. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  184. @overload
  185. def __call__(
  186. self, __other: unsignedinteger[_NBit2]
  187. ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
  188. class _UnsignedIntDivMod(Protocol[_NBit1]):
  189. @overload
  190. def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
  191. @overload
  192. def __call__(
  193. self, __other: Union[int, signedinteger[Any]]
  194. ) -> _2Tuple[Any]: ...
  195. @overload
  196. def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
  197. @overload
  198. def __call__(
  199. self, __other: unsignedinteger[_NBit2]
  200. ) -> _2Tuple[unsignedinteger[Union[_NBit1, _NBit2]]]: ...
  201. class _SignedIntOp(Protocol[_NBit1]):
  202. @overload
  203. def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
  204. @overload
  205. def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
  206. @overload
  207. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  208. @overload
  209. def __call__(
  210. self, __other: complex
  211. ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
  212. @overload
  213. def __call__(
  214. self, __other: signedinteger[_NBit2]
  215. ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
  216. class _SignedIntBitOp(Protocol[_NBit1]):
  217. @overload
  218. def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
  219. @overload
  220. def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
  221. @overload
  222. def __call__(
  223. self, __other: signedinteger[_NBit2]
  224. ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
  225. class _SignedIntMod(Protocol[_NBit1]):
  226. @overload
  227. def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
  228. @overload
  229. def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
  230. @overload
  231. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  232. @overload
  233. def __call__(
  234. self, __other: signedinteger[_NBit2]
  235. ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
  236. class _SignedIntDivMod(Protocol[_NBit1]):
  237. @overload
  238. def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
  239. @overload
  240. def __call__(self, __other: int) -> _2Tuple[signedinteger[Union[_NBit1, _NBitInt]]]: ...
  241. @overload
  242. def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
  243. @overload
  244. def __call__(
  245. self, __other: signedinteger[_NBit2]
  246. ) -> _2Tuple[signedinteger[Union[_NBit1, _NBit2]]]: ...
  247. class _FloatOp(Protocol[_NBit1]):
  248. @overload
  249. def __call__(self, __other: bool) -> floating[_NBit1]: ...
  250. @overload
  251. def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
  252. @overload
  253. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  254. @overload
  255. def __call__(
  256. self, __other: complex
  257. ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
  258. @overload
  259. def __call__(
  260. self, __other: Union[integer[_NBit2], floating[_NBit2]]
  261. ) -> floating[Union[_NBit1, _NBit2]]: ...
  262. class _FloatMod(Protocol[_NBit1]):
  263. @overload
  264. def __call__(self, __other: bool) -> floating[_NBit1]: ...
  265. @overload
  266. def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
  267. @overload
  268. def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
  269. @overload
  270. def __call__(
  271. self, __other: Union[integer[_NBit2], floating[_NBit2]]
  272. ) -> floating[Union[_NBit1, _NBit2]]: ...
  273. class _FloatDivMod(Protocol[_NBit1]):
  274. @overload
  275. def __call__(self, __other: bool) -> _2Tuple[floating[_NBit1]]: ...
  276. @overload
  277. def __call__(self, __other: int) -> _2Tuple[floating[Union[_NBit1, _NBitInt]]]: ...
  278. @overload
  279. def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
  280. @overload
  281. def __call__(
  282. self, __other: Union[integer[_NBit2], floating[_NBit2]]
  283. ) -> _2Tuple[floating[Union[_NBit1, _NBit2]]]: ...
  284. class _ComplexOp(Protocol[_NBit1]):
  285. @overload
  286. def __call__(self, __other: bool) -> complexfloating[_NBit1, _NBit1]: ...
  287. @overload
  288. def __call__(self, __other: int) -> complexfloating[Union[_NBit1, _NBitInt], Union[_NBit1, _NBitInt]]: ...
  289. @overload
  290. def __call__(
  291. self, __other: Union[float, complex]
  292. ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
  293. @overload
  294. def __call__(
  295. self,
  296. __other: Union[
  297. integer[_NBit2],
  298. floating[_NBit2],
  299. complexfloating[_NBit2, _NBit2],
  300. ]
  301. ) -> complexfloating[Union[_NBit1, _NBit2], Union[_NBit1, _NBit2]]: ...
  302. class _NumberOp(Protocol):
  303. def __call__(self, __other: _NumberLike_co) -> Any: ...
  304. class _ComparisonOp(Protocol[_T1, _T2]):
  305. @overload
  306. def __call__(self, __other: _T1) -> bool_: ...
  307. @overload
  308. def __call__(self, __other: _T2) -> NDArray[bool_]: ...
  309. else:
  310. _BoolOp = Any
  311. _BoolBitOp = Any
  312. _BoolSub = Any
  313. _BoolTrueDiv = Any
  314. _BoolMod = Any
  315. _BoolDivMod = Any
  316. _TD64Div = Any
  317. _IntTrueDiv = Any
  318. _UnsignedIntOp = Any
  319. _UnsignedIntBitOp = Any
  320. _UnsignedIntMod = Any
  321. _UnsignedIntDivMod = Any
  322. _SignedIntOp = Any
  323. _SignedIntBitOp = Any
  324. _SignedIntMod = Any
  325. _SignedIntDivMod = Any
  326. _FloatOp = Any
  327. _FloatMod = Any
  328. _FloatDivMod = Any
  329. _ComplexOp = Any
  330. _NumberOp = Any
  331. _ComparisonOp = Any