summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBas van Beek <43369155+BvB93@users.noreply.github.com>2020-06-15 04:49:42 +0200
committerGitHub <noreply@github.com>2020-06-14 19:49:42 -0700
commitf3ff68e5fe874f9626fcbe85000549083f4f222a (patch)
tree2f670e7df73d36f95edc660a6082136841ce9f9f
parent9e85b3701f80a5e48ca5e7fc5281af2aba591870 (diff)
downloadnumpy-f3ff68e5fe874f9626fcbe85000549083f4f222a.tar.gz
MAINT: changed np.generic arguments to positional-only (#16583)
Closes https://github.com/numpy/numpy/issues/16577. Make the arguments of `np.generic` constructors positional-only. Also fix the constructor of`np.bytes_`: encoding is only supported when supplied a string. The opposite holds for `np.str_`.
-rw-r--r--numpy/__init__.pyi46
-rw-r--r--numpy/tests/typing/fail/scalars.py14
-rw-r--r--numpy/tests/typing/pass/scalars.py4
3 files changed, 41 insertions, 23 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi
index 4aab0bbfa..f9218391e 100644
--- a/numpy/__init__.pyi
+++ b/numpy/__init__.pyi
@@ -387,18 +387,18 @@ class _real_generic(generic): # type: ignore
class number(generic): ... # type: ignore
class bool_(_real_generic):
- def __init__(self, value: object = ...) -> None: ...
+ def __init__(self, __value: object = ...) -> None: ...
class object_(generic):
- def __init__(self, value: object = ...) -> None: ...
+ def __init__(self, __value: object = ...) -> None: ...
class datetime64:
@overload
def __init__(
- self, _data: Union[datetime64, str, dt.datetime] = ..., _format: str = ...
+ self, __value: Union[datetime64, str, dt.datetime] = ..., __format: str = ...
) -> None: ...
@overload
- def __init__(self, _data: int, _format: str) -> None: ...
+ def __init__(self, __value: int, __format: str) -> None: ...
def __add__(self, other: Union[timedelta64, int]) -> datetime64: ...
def __sub__(self, other: Union[timedelta64, datetime64, int]) -> timedelta64: ...
@@ -406,19 +406,19 @@ class integer(number, _real_generic): ... # type: ignore
class signedinteger(integer): ... # type: ignore
class int8(signedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class int16(signedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class int32(signedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class int64(signedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class timedelta64(signedinteger):
- def __init__(self, _data: Any = ..., _format: str = ...) -> None: ...
+ def __init__(self, __value: Any = ..., __format: str = ...) -> None: ...
@overload
def __add__(self, other: Union[timedelta64, int]) -> timedelta64: ...
@overload
@@ -438,34 +438,34 @@ class timedelta64(signedinteger):
class unsignedinteger(integer): ... # type: ignore
class uint8(unsignedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class uint16(unsignedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class uint32(unsignedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class uint64(unsignedinteger):
- def __init__(self, value: SupportsInt = ...) -> None: ...
+ def __init__(self, __value: SupportsInt = ...) -> None: ...
class inexact(number): ... # type: ignore
class floating(inexact, _real_generic): ... # type: ignore
class float16(floating):
- def __init__(self, value: SupportsFloat = ...) -> None: ...
+ def __init__(self, __value: SupportsFloat = ...) -> None: ...
class float32(floating):
- def __init__(self, value: SupportsFloat = ...) -> None: ...
+ def __init__(self, __value: SupportsFloat = ...) -> None: ...
class float64(floating):
- def __init__(self, value: SupportsFloat = ...) -> None: ...
+ def __init__(self, __value: SupportsFloat = ...) -> None: ...
class complexfloating(inexact): ... # type: ignore
class complex64(complexfloating):
def __init__(
- self, value: Union[SupportsInt, SupportsFloat, SupportsComplex] = ...
+ self, __value: Union[SupportsInt, SupportsFloat, SupportsComplex] = ...
) -> None: ...
@property
def real(self) -> float32: ...
@@ -474,7 +474,7 @@ class complex64(complexfloating):
class complex128(complexfloating):
def __init__(
- self, value: Union[SupportsInt, SupportsFloat, SupportsComplex] = ...
+ self, __value: Union[SupportsInt, SupportsFloat, SupportsComplex] = ...
) -> None: ...
@property
def real(self) -> float64: ...
@@ -484,24 +484,24 @@ class complex128(complexfloating):
class flexible(_real_generic): ... # type: ignore
class void(flexible):
- def __init__(self, value: Union[int, integer, bool_, bytes, bytes_]): ...
+ def __init__(self, __value: Union[int, integer, bool_, bytes, bytes_]): ...
class character(_real_generic): ... # type: ignore
class bytes_(character):
@overload
- def __init__(self, value: object = ...) -> None: ...
+ def __init__(self, __value: object = ...) -> None: ...
@overload
def __init__(
- self, value: object, encoding: str = ..., errors: str = ...
+ self, __value: Union[str, str_], encoding: str = ..., errors: str = ...
) -> None: ...
class str_(character):
@overload
- def __init__(self, value: object = ...) -> None: ...
+ def __init__(self, __value: object = ...) -> None: ...
@overload
def __init__(
- self, value: object, encoding: str = ..., errors: str = ...
+ self, __value: Union[bytes, bytes_], encoding: str = ..., errors: str = ...
) -> None: ...
# TODO(alan): Platform dependent types
diff --git a/numpy/tests/typing/fail/scalars.py b/numpy/tests/typing/fail/scalars.py
index 0dfc55124..5d7221895 100644
--- a/numpy/tests/typing/fail/scalars.py
+++ b/numpy/tests/typing/fail/scalars.py
@@ -65,3 +65,17 @@ np.floating(1) # E: Cannot instantiate abstract class
np.complexfloating(1) # E: Cannot instantiate abstract class
np.character("test") # E: Cannot instantiate abstract class
np.flexible(b"test") # E: Cannot instantiate abstract class
+
+np.float64(value=0.0) # E: Unexpected keyword argument
+np.int64(value=0) # E: Unexpected keyword argument
+np.uint64(value=0) # E: Unexpected keyword argument
+np.complex128(value=0.0j) # E: Unexpected keyword argument
+np.str_(value='bob') # E: No overload variant
+np.bytes_(value=b'test') # E: No overload variant
+np.void(value=b'test') # E: Unexpected keyword argument
+np.bool_(value=True) # E: Unexpected keyword argument
+np.datetime64(value="2019") # E: No overload variant
+np.timedelta64(value=0) # E: Unexpected keyword argument
+
+np.bytes_(b"hello", encoding='utf-8') # E: No overload variant
+np.str_("hello", encoding='utf-8') # E: No overload variant
diff --git a/numpy/tests/typing/pass/scalars.py b/numpy/tests/typing/pass/scalars.py
index bd055673b..7de182626 100644
--- a/numpy/tests/typing/pass/scalars.py
+++ b/numpy/tests/typing/pass/scalars.py
@@ -34,7 +34,11 @@ np.float32(16)
np.float64(3.0)
np.bytes_(b"hello")
+np.bytes_("hello", 'utf-8')
+np.bytes_("hello", encoding='utf-8')
np.str_("hello")
+np.str_(b"hello", 'utf-8')
+np.str_(b"hello", encoding='utf-8')
# Protocols
float(np.int8(4))