simple.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """Simple expression that should pass with mypy."""
  2. import operator
  3. import numpy as np
  4. from collections.abc import Iterable
  5. # Basic checks
  6. array = np.array([1, 2])
  7. def ndarray_func(x):
  8. # type: (np.ndarray) -> np.ndarray
  9. return x
  10. ndarray_func(np.array([1, 2]))
  11. array == 1
  12. array.dtype == float
  13. # Dtype construction
  14. np.dtype(float)
  15. np.dtype(np.float64)
  16. np.dtype(None)
  17. np.dtype("float64")
  18. np.dtype(np.dtype(float))
  19. np.dtype(("U", 10))
  20. np.dtype((np.int32, (2, 2)))
  21. # Define the arguments on the previous line to prevent bidirectional
  22. # type inference in mypy from broadening the types.
  23. two_tuples_dtype = [("R", "u1"), ("G", "u1"), ("B", "u1")]
  24. np.dtype(two_tuples_dtype)
  25. three_tuples_dtype = [("R", "u1", 2)]
  26. np.dtype(three_tuples_dtype)
  27. mixed_tuples_dtype = [("R", "u1"), ("G", np.unicode_, 1)]
  28. np.dtype(mixed_tuples_dtype)
  29. shape_tuple_dtype = [("R", "u1", (2, 2))]
  30. np.dtype(shape_tuple_dtype)
  31. shape_like_dtype = [("R", "u1", (2, 2)), ("G", np.unicode_, 1)]
  32. np.dtype(shape_like_dtype)
  33. object_dtype = [("field1", object)]
  34. np.dtype(object_dtype)
  35. np.dtype((np.int32, (np.int8, 4)))
  36. # Dtype comparison
  37. np.dtype(float) == float
  38. np.dtype(float) != np.float64
  39. np.dtype(float) < None
  40. np.dtype(float) <= "float64"
  41. np.dtype(float) > np.dtype(float)
  42. np.dtype(float) >= np.dtype(("U", 10))
  43. # Iteration and indexing
  44. def iterable_func(x):
  45. # type: (Iterable) -> Iterable
  46. return x
  47. iterable_func(array)
  48. [element for element in array]
  49. iter(array)
  50. zip(array, array)
  51. array[1]
  52. array[:]
  53. array[...]
  54. array[:] = 0
  55. array_2d = np.ones((3, 3))
  56. array_2d[:2, :2]
  57. array_2d[..., 0]
  58. array_2d[:2, :2] = 0
  59. # Other special methods
  60. len(array)
  61. str(array)
  62. array_scalar = np.array(1)
  63. int(array_scalar)
  64. float(array_scalar)
  65. # currently does not work due to https://github.com/python/typeshed/issues/1904
  66. # complex(array_scalar)
  67. bytes(array_scalar)
  68. operator.index(array_scalar)
  69. bool(array_scalar)
  70. # comparisons
  71. array < 1
  72. array <= 1
  73. array == 1
  74. array != 1
  75. array > 1
  76. array >= 1
  77. 1 < array
  78. 1 <= array
  79. 1 == array
  80. 1 != array
  81. 1 > array
  82. 1 >= array
  83. # binary arithmetic
  84. array + 1
  85. 1 + array
  86. array += 1
  87. array - 1
  88. 1 - array
  89. array -= 1
  90. array * 1
  91. 1 * array
  92. array *= 1
  93. nonzero_array = np.array([1, 2])
  94. array / 1
  95. 1 / nonzero_array
  96. float_array = np.array([1.0, 2.0])
  97. float_array /= 1
  98. array // 1
  99. 1 // nonzero_array
  100. array //= 1
  101. array % 1
  102. 1 % nonzero_array
  103. array %= 1
  104. divmod(array, 1)
  105. divmod(1, nonzero_array)
  106. array ** 1
  107. 1 ** array
  108. array **= 1
  109. array << 1
  110. 1 << array
  111. array <<= 1
  112. array >> 1
  113. 1 >> array
  114. array >>= 1
  115. array & 1
  116. 1 & array
  117. array &= 1
  118. array ^ 1
  119. 1 ^ array
  120. array ^= 1
  121. array | 1
  122. 1 | array
  123. array |= 1
  124. # unary arithmetic
  125. -array
  126. +array
  127. abs(array)
  128. ~array
  129. # Other methods
  130. np.array([1, 2]).transpose()