From 12a4df466b7a728834c79d9bef6209e38785551a Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 31 Jan 2021 14:16:37 -0600 Subject: [PATCH 1/2] Adapt type annotations to numpy 1.20, add compat note --- README.rst | 8 ++++++++ pytato/array.py | 52 +++++++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index ca4df16..9398741 100644 --- a/README.rst +++ b/README.rst @@ -37,3 +37,11 @@ Example:: Pytato is licensed to you under the MIT/X Consortium license. See the `documentation `__ for further details + +Numpy compatibility +------------------- + +Pytato is written to pose no particular restrictions on the version of numpy +used for execution. To use mypy-based type checking on Pytato itself or +packages using Pytato, numpy 1.20 or newer is required, due to the +typing-based changes to numpy in that release. diff --git a/pytato/array.py b/pytato/array.py index 36518da..77a9e67 100644 --- a/pytato/array.py +++ b/pytato/array.py @@ -175,7 +175,8 @@ import pytato.scalar_expr as scalar_expr from pytato.scalar_expr import ScalarExpression, IntegralScalarExpression -# Get a type variable that represents the type of '...' +# {{{ get a type variable that represents the type of '...' + # https://github.com/python/typing/issues/684#issuecomment-548203158 if TYPE_CHECKING: from enum import Enum @@ -187,6 +188,13 @@ if TYPE_CHECKING: else: EllipsisType = type(Ellipsis) +# }}} + +if TYPE_CHECKING: + _dtype_any = np.dtype[Any] +else: + _dtype_any = np.dtype + # {{{ namespace @@ -334,16 +342,16 @@ def normalize_shape( # {{{ array inteface SliceItem = Union[int, slice, None, EllipsisType] -DtypeOrScalar = Union[np.dtype, Number] +DtypeOrScalar = Union[_dtype_any, Number] -def _truediv_result_type(arg1: DtypeOrScalar, arg2: DtypeOrScalar) -> np.dtype: +def _truediv_result_type(arg1: DtypeOrScalar, arg2: DtypeOrScalar) -> np.dtype[Any]: dtype = np.result_type(arg1, arg2) # See: test_true_divide in numpy/core/tests/test_ufunc.py if dtype.kind in "iu": - return np.float64 + return np.dtype(np.float64) else: - return dtype + return cast(_dtype_any, dtype) class Array(Taggable): @@ -443,7 +451,7 @@ class Array(Taggable): return product(self.shape) @property - def dtype(self) -> np.dtype: + def dtype(self) -> np.dtype[Any]: raise NotImplementedError def named(self, name: str) -> Array: @@ -575,7 +583,7 @@ class Array(Taggable): def _binary_op(self, op: Any, other: Union[Array, Number], - get_result_type: Callable[[DtypeOrScalar, DtypeOrScalar], np.dtype] = np.result_type, # noqa + get_result_type: Callable[[DtypeOrScalar, DtypeOrScalar], np.dtype[Any]] = np.result_type, # noqa reverse: bool = False) -> Array: def add_indices(val: prim.Expression) -> prim.Expression: @@ -665,7 +673,7 @@ class _SuppliedShapeAndDtypeMixin(object): def __init__(self, shape: ShapeType, - dtype: np.dtype, + dtype: np.dtype[Any], **kwargs: Any): # https://github.com/python/mypy/issues/5887 super().__init__(**kwargs) # type: ignore @@ -677,7 +685,7 @@ class _SuppliedShapeAndDtypeMixin(object): return self._shape @property - def dtype(self) -> np.dtype: + def dtype(self) -> np.dtype[Any]: return self._dtype # }}} @@ -807,7 +815,7 @@ class IndexLambda(_SuppliedShapeAndDtypeMixin, Array): namespace: Namespace, expr: prim.Expression, shape: ShapeType, - dtype: np.dtype, + dtype: np.dtype[Any], bindings: Optional[Dict[str, Array]] = None, tags: TagsType = frozenset()): @@ -915,8 +923,8 @@ class MatrixProduct(Array): assert False @property - def dtype(self) -> np.dtype: - return np.result_type(self.x1.dtype, self.x2.dtype) + def dtype(self) -> np.dtype[Any]: + return cast(_dtype_any, np.result_type(self.x1.dtype, self.x2.dtype)) # }}} @@ -952,8 +960,9 @@ class Stack(Array): return self.arrays[0].namespace @property - def dtype(self) -> np.dtype: - return np.result_type(*(arr.dtype for arr in self.arrays)) + def dtype(self) -> np.dtype[Any]: + return cast(_dtype_any, + np.result_type(*(arr.dtype for arr in self.arrays))) @property def shape(self) -> ShapeType: @@ -995,8 +1004,9 @@ class Concatenate(Array): return self.arrays[0].namespace @property - def dtype(self) -> np.dtype: - return np.result_type(*(arr.dtype for arr in self.arrays)) + def dtype(self) -> np.dtype[Any]: + return cast(_dtype_any, + np.result_type(*(arr.dtype for arr in self.arrays))) @property def shape(self) -> ShapeType: @@ -1045,7 +1055,7 @@ class IndexRemappingBase(Array): self.array = array @property - def dtype(self) -> np.dtype: + def dtype(self) -> np.dtype[Any]: return self.array.dtype @property @@ -1255,7 +1265,7 @@ class DataInterface(Protocol): """ shape: ShapeType - dtype: np.dtype + dtype: np.dtype[Any] class DataWrapper(InputArgumentBase): @@ -1292,7 +1302,7 @@ class DataWrapper(InputArgumentBase): return self._shape @property - def dtype(self) -> np.dtype: + def dtype(self) -> np.dtype[Any]: return self.data.dtype # }}} @@ -1314,7 +1324,7 @@ class Placeholder(_SuppliedShapeAndDtypeMixin, InputArgumentBase): namespace: Namespace, name: str, shape: ShapeType, - dtype: np.dtype, + dtype: np.dtype[Any], tags: TagsType = frozenset()): """Should not be called directly. Use :func:`make_placeholder` instead. @@ -1342,7 +1352,7 @@ class SizeParam(InputArgumentBase): return () @property - def dtype(self) -> np.dtype: + def dtype(self) -> np.dtype[Any]: return np.dtype(np.intp) # }}} -- GitLab From 2838725d0d70a73e6690219d7457a17bc5dbd129 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 31 Jan 2021 14:16:57 -0600 Subject: [PATCH 2/2] Black setup.py --- setup.py | 65 +++++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/setup.py b/setup.py index 8655fc5..3c4e24c 100644 --- a/setup.py +++ b/setup.py @@ -8,38 +8,35 @@ version_file_name = "pytato/version.py" with open(version_file_name) as version_file: version_file_contents = version_file.read() -exec(compile(version_file_contents, version_file_name, 'exec'), ver_dic) +exec(compile(version_file_contents, version_file_name, "exec"), ver_dic) -setup(name="pytato", - version=ver_dic["VERSION_TEXT"], - description="Get Descriptions of Array Computations via Lazy Evaluation", - long_description=open("README.rst", "r").read(), - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'Intended Audience :: Other Audience', - 'Intended Audience :: Science/Research', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Topic :: Scientific/Engineering', - 'Topic :: Scientific/Engineering :: Information Analysis', - 'Topic :: Scientific/Engineering :: Mathematics', - 'Topic :: Scientific/Engineering :: Visualization', - 'Topic :: Software Development :: Libraries', - ], - - python_requires="~=3.8", - install_requires=[ - "loopy>=2020.2", - "pytools>=2021.1" - ], - - author="Andreas Kloeckner, Matt Wala, Xiaoyu Wei", - url="https://github.com/inducer/pytato", - author_email="inform@tiker.net", - license="MIT", - packages=find_packages()) +setup( + name="pytato", + version=ver_dic["VERSION_TEXT"], + description="Get Descriptions of Array Computations via Lazy Evaluation", + long_description=open("README.rst", "r").read(), + classifiers=[ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Intended Audience :: Other Audience", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Topic :: Scientific/Engineering", + "Topic :: Scientific/Engineering :: Information Analysis", + "Topic :: Scientific/Engineering :: Mathematics", + "Topic :: Scientific/Engineering :: Visualization", + "Topic :: Software Development :: Libraries", + ], + python_requires="~=3.8", + install_requires=["loopy>=2020.2", "pytools>=2021.1"], + author="Andreas Kloeckner, Matt Wala, Xiaoyu Wei", + url="https://github.com/inducer/pytato", + author_email="inform@tiker.net", + license="MIT", + packages=find_packages(), +) -- GitLab