From 5c04e06dacb714fb8381db003eb0f22c08c91417 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Mon, 3 Jan 2022 23:03:10 +0100 Subject: TYP: add a few type annotations to `numpy.array_api.Array` This fixes the majority of the complaints for `$ mypy numpy/array_api`. The comment indicating that one fix is blocked by lack of support in Mypy for `NotImplemented` is responsible for another several dozen errors. [skip ci] --- numpy/array_api/_array_object.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'numpy/array_api/_array_object.py') diff --git a/numpy/array_api/_array_object.py b/numpy/array_api/_array_object.py index 75baf34b0..900a0ae1e 100644 --- a/numpy/array_api/_array_object.py +++ b/numpy/array_api/_array_object.py @@ -55,6 +55,7 @@ class Array: functions, such as asarray(). """ + _array: np.ndarray # Use a custom constructor instead of __init__, as manually initializing # this class is not supported API. @@ -124,6 +125,9 @@ class Array: # spec in places where it either deviates from or is more strict than # NumPy behavior + # NOTE: no valid type annotation possible. E.g `Union[Array, + # NotImplemented]` is forbidden, see https://github.com/python/mypy/issues/363 + # Maybe change returned object to `Literal['NotImplemented']`? def _check_allowed_dtypes(self, other, dtype_category, op): """ Helper function for operators to only allow specific input dtypes @@ -200,7 +204,7 @@ class Array: return Array._new(np.array(scalar, self.dtype)) @staticmethod - def _normalize_two_args(x1, x2): + def _normalize_two_args(x1, x2) -> Tuple[Array, Array]: """ Normalize inputs to two arg functions to fix type promotion rules -- cgit v1.2.1 From 890c0cfe15f0e3325b57e44f9b9a9f78f9797f25 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Tue, 4 Jan 2022 08:17:10 +0100 Subject: TYP: accept review comment on ignoring NotImplemented in type checking Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com> --- numpy/array_api/_array_object.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'numpy/array_api/_array_object.py') diff --git a/numpy/array_api/_array_object.py b/numpy/array_api/_array_object.py index 900a0ae1e..93ae10603 100644 --- a/numpy/array_api/_array_object.py +++ b/numpy/array_api/_array_object.py @@ -125,10 +125,7 @@ class Array: # spec in places where it either deviates from or is more strict than # NumPy behavior - # NOTE: no valid type annotation possible. E.g `Union[Array, - # NotImplemented]` is forbidden, see https://github.com/python/mypy/issues/363 - # Maybe change returned object to `Literal['NotImplemented']`? - def _check_allowed_dtypes(self, other, dtype_category, op): + def _check_allowed_dtypes(self, other: bool | int | float | Array, dtype_category: str, op: str) -> Array: """ Helper function for operators to only allow specific input dtypes -- cgit v1.2.1