scalars.pyi 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import sys
  2. import numpy as np
  3. f2: np.float16
  4. f8: np.float64
  5. c8: np.complex64
  6. # Construction
  7. np.float32(3j) # E: incompatible type
  8. # Technically the following examples are valid NumPy code. But they
  9. # are not considered a best practice, and people who wish to use the
  10. # stubs should instead do
  11. #
  12. # np.array([1.0, 0.0, 0.0], dtype=np.float32)
  13. # np.array([], dtype=np.complex64)
  14. #
  15. # See e.g. the discussion on the mailing list
  16. #
  17. # https://mail.python.org/pipermail/numpy-discussion/2020-April/080566.html
  18. #
  19. # and the issue
  20. #
  21. # https://github.com/numpy/numpy-stubs/issues/41
  22. #
  23. # for more context.
  24. np.float32([1.0, 0.0, 0.0]) # E: incompatible type
  25. np.complex64([]) # E: incompatible type
  26. np.complex64(1, 2) # E: Too many arguments
  27. # TODO: protocols (can't check for non-existent protocols w/ __getattr__)
  28. np.datetime64(0) # E: No overload variant
  29. class A:
  30. def __float__(self):
  31. return 1.0
  32. np.int8(A()) # E: incompatible type
  33. np.int16(A()) # E: incompatible type
  34. np.int32(A()) # E: incompatible type
  35. np.int64(A()) # E: incompatible type
  36. np.uint8(A()) # E: incompatible type
  37. np.uint16(A()) # E: incompatible type
  38. np.uint32(A()) # E: incompatible type
  39. np.uint64(A()) # E: incompatible type
  40. np.void("test") # E: No overload variant
  41. np.void("test", dtype=None) # E: No overload variant
  42. np.generic(1) # E: Cannot instantiate abstract class
  43. np.number(1) # E: Cannot instantiate abstract class
  44. np.integer(1) # E: Cannot instantiate abstract class
  45. np.inexact(1) # E: Cannot instantiate abstract class
  46. np.character("test") # E: Cannot instantiate abstract class
  47. np.flexible(b"test") # E: Cannot instantiate abstract class
  48. np.float64(value=0.0) # E: Unexpected keyword argument
  49. np.int64(value=0) # E: Unexpected keyword argument
  50. np.uint64(value=0) # E: Unexpected keyword argument
  51. np.complex128(value=0.0j) # E: Unexpected keyword argument
  52. np.str_(value='bob') # E: No overload variant
  53. np.bytes_(value=b'test') # E: No overload variant
  54. np.void(value=b'test') # E: No overload variant
  55. np.bool_(value=True) # E: Unexpected keyword argument
  56. np.datetime64(value="2019") # E: No overload variant
  57. np.timedelta64(value=0) # E: Unexpected keyword argument
  58. np.bytes_(b"hello", encoding='utf-8') # E: No overload variant
  59. np.str_("hello", encoding='utf-8') # E: No overload variant
  60. f8.item(1) # E: incompatible type
  61. f8.item((0, 1)) # E: incompatible type
  62. f8.squeeze(axis=1) # E: incompatible type
  63. f8.squeeze(axis=(0, 1)) # E: incompatible type
  64. f8.transpose(1) # E: incompatible type
  65. def func(a: np.float32) -> None: ...
  66. func(f2) # E: incompatible type
  67. func(f8) # E: incompatible type
  68. round(c8) # E: No overload variant
  69. c8.__getnewargs__() # E: Invalid self argument
  70. f2.__getnewargs__() # E: Invalid self argument
  71. f2.hex() # E: Invalid self argument
  72. np.float16.fromhex("0x0.0p+0") # E: Invalid self argument
  73. f2.__trunc__() # E: Invalid self argument
  74. f2.__getformat__("float") # E: Invalid self argument