scalars.py 2.4 KB

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