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.

140 lines
3.4 KiB

6 months ago
  1. from __future__ import annotations
  2. import sys
  3. from typing import (
  4. Any,
  5. overload,
  6. Sequence,
  7. TYPE_CHECKING,
  8. Union,
  9. TypeVar,
  10. Generic,
  11. )
  12. from numpy import (
  13. ndarray,
  14. dtype,
  15. generic,
  16. bool_,
  17. unsignedinteger,
  18. integer,
  19. floating,
  20. complexfloating,
  21. number,
  22. timedelta64,
  23. datetime64,
  24. object_,
  25. void,
  26. str_,
  27. bytes_,
  28. )
  29. from ._dtype_like import DTypeLike
  30. if sys.version_info >= (3, 8):
  31. from typing import Protocol
  32. HAVE_PROTOCOL = True
  33. else:
  34. try:
  35. from typing_extensions import Protocol
  36. except ImportError:
  37. HAVE_PROTOCOL = False
  38. else:
  39. HAVE_PROTOCOL = True
  40. _T = TypeVar("_T")
  41. _ScalarType = TypeVar("_ScalarType", bound=generic)
  42. _DType = TypeVar("_DType", bound="dtype[Any]")
  43. _DType_co = TypeVar("_DType_co", covariant=True, bound="dtype[Any]")
  44. if TYPE_CHECKING or HAVE_PROTOCOL:
  45. # The `_SupportsArray` protocol only cares about the default dtype
  46. # (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
  47. # array.
  48. # Concrete implementations of the protocol are responsible for adding
  49. # any and all remaining overloads
  50. class _SupportsArray(Protocol[_DType_co]):
  51. def __array__(self) -> ndarray[Any, _DType_co]: ...
  52. else:
  53. class _SupportsArray(Generic[_DType_co]):
  54. pass
  55. # TODO: Wait for support for recursive types
  56. _NestedSequence = Union[
  57. _T,
  58. Sequence[_T],
  59. Sequence[Sequence[_T]],
  60. Sequence[Sequence[Sequence[_T]]],
  61. Sequence[Sequence[Sequence[Sequence[_T]]]],
  62. ]
  63. _RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]]
  64. # A union representing array-like objects; consists of two typevars:
  65. # One representing types that can be parametrized w.r.t. `np.dtype`
  66. # and another one for the rest
  67. _ArrayLike = Union[
  68. _NestedSequence[_SupportsArray[_DType]],
  69. _NestedSequence[_T],
  70. ]
  71. # TODO: support buffer protocols once
  72. #
  73. # https://bugs.python.org/issue27501
  74. #
  75. # is resolved. See also the mypy issue:
  76. #
  77. # https://github.com/python/typing/issues/593
  78. ArrayLike = Union[
  79. _RecursiveSequence,
  80. _ArrayLike[
  81. dtype,
  82. Union[bool, int, float, complex, str, bytes]
  83. ],
  84. ]
  85. # `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
  86. # given the casting rules `same_kind`
  87. _ArrayLikeBool_co = _ArrayLike[
  88. "dtype[bool_]",
  89. bool,
  90. ]
  91. _ArrayLikeUInt_co = _ArrayLike[
  92. "dtype[Union[bool_, unsignedinteger[Any]]]",
  93. bool,
  94. ]
  95. _ArrayLikeInt_co = _ArrayLike[
  96. "dtype[Union[bool_, integer[Any]]]",
  97. Union[bool, int],
  98. ]
  99. _ArrayLikeFloat_co = _ArrayLike[
  100. "dtype[Union[bool_, integer[Any], floating[Any]]]",
  101. Union[bool, int, float],
  102. ]
  103. _ArrayLikeComplex_co = _ArrayLike[
  104. "dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]",
  105. Union[bool, int, float, complex],
  106. ]
  107. _ArrayLikeNumber_co = _ArrayLike[
  108. "dtype[Union[bool_, number[Any]]]",
  109. Union[bool, int, float, complex],
  110. ]
  111. _ArrayLikeTD64_co = _ArrayLike[
  112. "dtype[Union[bool_, integer[Any], timedelta64]]",
  113. Union[bool, int],
  114. ]
  115. _ArrayLikeDT64_co = _NestedSequence[_SupportsArray["dtype[datetime64]"]]
  116. _ArrayLikeObject_co = _NestedSequence[_SupportsArray["dtype[object_]"]]
  117. _ArrayLikeVoid_co = _NestedSequence[_SupportsArray["dtype[void]"]]
  118. _ArrayLikeStr_co = _ArrayLike[
  119. "dtype[str_]",
  120. str,
  121. ]
  122. _ArrayLikeBytes_co = _ArrayLike[
  123. "dtype[bytes_]",
  124. bytes,
  125. ]
  126. _ArrayLikeInt = _ArrayLike[
  127. "dtype[integer[Any]]",
  128. int,
  129. ]