From 99ba291c9d965782b8772b329604131dad785a46 Mon Sep 17 00:00:00 2001 From: Rajat Dipta Biswas Date: Wed, 29 Apr 2020 11:35:44 +0530 Subject: [PATCH 1/3] Add more specific integer constructors Fixes #72 - Removed SupportsFloat from the classes int8, int16, int32, int64, uint8, uint16, uint32, and uint64 - Removed a test from tests/pass/scalars.py - Added several tests to tests/fail/scalars.py --- numpy-stubs/__init__.pyi | 33 +++++++++++++++++++++++++-------- tests/fail/scalars.py | 13 +++++++++++++ tests/pass/scalars.py | 1 - 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/numpy-stubs/__init__.pyi b/numpy-stubs/__init__.pyi index c51f3a6..453b65d 100644 --- a/numpy-stubs/__init__.pyi +++ b/numpy-stubs/__init__.pyi @@ -417,10 +417,18 @@ class datetime64: class integer(number, _real_generic): ... class signedinteger(integer): ... -class int8(signedinteger): ... -class int16(signedinteger): ... -class int32(signedinteger): ... -class int64(signedinteger): ... + +class int8(signedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class int16(signedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class int32(signedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class int64(signedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... class timedelta64(signedinteger): def __init__(self, _data: Any = ..., _format: str = ...) -> None: ... @@ -441,10 +449,19 @@ class timedelta64(signedinteger): def __mod__(self, other: timedelta64) -> timedelta64: ... class unsignedinteger(integer): ... -class uint8(unsignedinteger): ... -class uint16(unsignedinteger): ... -class uint32(unsignedinteger): ... -class uint64(unsignedinteger): ... + +class uint8(unsignedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class uint16(unsignedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class uint32(unsignedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + +class uint64(unsignedinteger): + def __init__(self, value: SupportsInt = ...) -> None: ... + class inexact(number): ... class floating(inexact, _real_generic): ... class float16(floating): ... diff --git a/tests/fail/scalars.py b/tests/fail/scalars.py index 38749ed..50ae477 100644 --- a/tests/fail/scalars.py +++ b/tests/fail/scalars.py @@ -37,3 +37,16 @@ td_64 / dt_64 # E: No overload td_64 % 1 # E: Unsupported operand types td_64 % dt_64 # E: Unsupported operand types + +class A: + def __float__(self): + return 1.0 + +np.int8(A()) # E: incompatible type +np.int16(A()) # E: incompatible type +np.int32(A()) # E: incompatible type +np.int64(A()) # E: incompatible type +np.uint8(A()) # E: incompatible type +np.uint16(A()) # E: incompatible type +np.uint32(A()) # E: incompatible type +np.uint64(A()) # E: incompatible type diff --git a/tests/pass/scalars.py b/tests/pass/scalars.py index 302b53d..43e226a 100644 --- a/tests/pass/scalars.py +++ b/tests/pass/scalars.py @@ -20,7 +20,6 @@ def __float__(self): np.int16(3.4) np.int32(4) np.int64(-1) -np.uint8(A()) np.uint32() np.float16(A()) From 7bed52df7654aa0fc2690d935009d4c45f6cc8fa Mon Sep 17 00:00:00 2001 From: Rajat Dipta Biswas Date: Wed, 29 Apr 2020 11:52:11 +0530 Subject: [PATCH 2/3] Update tests/fail/scalars.py with formatting Updated tests/fail/scalars.py with formatting done by black --- tests/fail/scalars.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/fail/scalars.py b/tests/fail/scalars.py index 50ae477..517e8f2 100644 --- a/tests/fail/scalars.py +++ b/tests/fail/scalars.py @@ -38,10 +38,12 @@ td_64 % 1 # E: Unsupported operand types td_64 % dt_64 # E: Unsupported operand types + class A: def __float__(self): return 1.0 + np.int8(A()) # E: incompatible type np.int16(A()) # E: incompatible type np.int32(A()) # E: incompatible type From 34e8ffa3ac256470bf529be8773b345eae00b975 Mon Sep 17 00:00:00 2001 From: Rajat Dipta Biswas Date: Wed, 29 Apr 2020 22:03:17 +0530 Subject: [PATCH 3/3] Update tests/pass/scalars.py - Added an uint8 test back - Created a new integer class definition - Corrected a float class definition --- tests/pass/scalars.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/pass/scalars.py b/tests/pass/scalars.py index 43e226a..c219bef 100644 --- a/tests/pass/scalars.py +++ b/tests/pass/scalars.py @@ -7,9 +7,14 @@ def __complex__(self): return 3j +class B: + def __int__(self): + return 4 + + class A: def __float__(self): - return 4 + return 4.0 np.complex32(3) @@ -20,6 +25,7 @@ def __float__(self): np.int16(3.4) np.int32(4) np.int64(-1) +np.uint8(B()) np.uint32() np.float16(A())