From 40c8e9d95dbe02c7abdfd05ddbbd73d46a2c5652 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 31 Jul 2024 15:50:24 -0700 Subject: [PATCH] remove nptyping dependency, update CI, remove old numpy types --- .github/workflows/lint.yml | 28 +++++ .github/workflows/tests.yml | 49 ++++++--- pyproject.toml | 3 +- src/numpydantic/dtype.py | 2 - src/numpydantic/schema.py | 2 +- src/numpydantic/vendor/nptyping/__init__.py | 7 -- src/numpydantic/vendor/nptyping/typing_.py | 12 ++- src/numpydantic/vendor/nptyping/typing_.pyi | 114 -------------------- tests/test_ndarray.py | 3 +- tox.ini | 14 +++ 10 files changed, 90 insertions(+), 144 deletions(-) create mode 100644 .github/workflows/lint.yml delete mode 100644 src/numpydantic/vendor/nptyping/typing_.pyi create mode 100644 tox.ini diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..aeb668b --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,28 @@ +name: Lint + +on: + push: + branches: + - main + pull_request: + branches: + - main + + +permissions: + contents: read + +jobs: + ruff: + name: Ruff Linting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: chartboost/ruff-action@v1 + + black: + name: Black Formatting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: psf/black@stable diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 31f586e..2c7e079 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,14 +2,34 @@ name: Tests on: push: + branches: + - main + pull_request: + branches: + - main jobs: test: strategy: matrix: - python-version: ["3.9", "3.11", "3.12"] + platform: ["ubuntu-latest", "macos-latest", "windows-latest"] + numpy-version: ["<2.0.0", ">=2.0.0"] + python-version: ["3.9", "3.10", "3.11", "3.12"] + exclude: + - numpy-version: "<2.0.0" + python-version: "3.10" + - numpy-version: "<2.0.0" + python-version: "3.11" + - platform: "macos-latest" + python-version: "3.10" + - platform: "macos-latest" + python-version: "3.11" + - platform: "windows-latest" + python-version: "3.10" + - platform: "windows-latest" + python-version: "3.11" - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 @@ -23,19 +43,24 @@ jobs: - name: Install dependencies run: pip install -e ".[tests,linkml]" + - name: Install numpy version + run: pip install "numpy${{ matrix.numpy-version }}" + - name: Run Tests run: pytest - - name: Report coverage - if: ${{ matrix.python-version }} == "3.11" - run: "coveralls --service=github" - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Coveralls Parallel + uses: coverallsapp/github-action@v2 + with: + flag-name: run-${{ join(matrix.*, '-') }} + parallel: true - lint: + finish-coverage: + needs: test + if: ${{ always() }} runs-on: ubuntu-latest - continue-on-error: true steps: - - uses: actions/checkout@v3 - - uses: chartboost/ruff-action@v1 - - uses: psf/black@stable \ No newline at end of file + - name: Coveralls Finished + uses: coverallsapp/github-action@v2 + with: + parallel-finished: true \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 05b97ea..12a1b50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,8 +7,8 @@ authors = [ ] dependencies = [ "pydantic>=2.3.0", - "nptyping>=2.5.0", "numpy>=1.24.0", + "typing-extensions>=4.11.0;python_version<'3.11'" ] homepage = "https://numpydantic.readthedocs.io" requires-python = "<4.0,>=3.9" @@ -40,7 +40,6 @@ tests = [ "coverage>=6.1.1", "pytest-cov<5.0.0,>=4.1.0", "coveralls<4.0.0,>=3.3.1", - "typing-extensions>=4.11.0;python_version<'3.11'", ] docs = [ "sphinx<8.0.0,>=7.2.6", diff --git a/src/numpydantic/dtype.py b/src/numpydantic/dtype.py index 4c0cc80..12d766a 100644 --- a/src/numpydantic/dtype.py +++ b/src/numpydantic/dtype.py @@ -78,14 +78,12 @@ Float = ( ) Floating = Float -ComplexFloating = np.complexfloating Complex64 = np.complex64 Complex128 = np.complex128 CSingle = np.csingle CDouble = np.cdouble CLongDouble = np.clongdouble Complex = ( - np.complexfloating, np.complex64, np.complex128, np.csingle, diff --git a/src/numpydantic/schema.py b/src/numpydantic/schema.py index 8e7e1bf..bf3fd90 100644 --- a/src/numpydantic/schema.py +++ b/src/numpydantic/schema.py @@ -50,7 +50,7 @@ def _lol_dtype(dtype: DtypeType, _handler: _handler_type) -> CoreSchema: if isinstance(dtype, tuple): # if it's a meta-type that refers to a generic float/int, just make that - if dtype == dt.Float: + if dtype in (dt.Float, dt.Number): array_type = core_schema.float_schema() elif dtype == dt.Integer: array_type = core_schema.int_schema() diff --git a/src/numpydantic/vendor/nptyping/__init__.py b/src/numpydantic/vendor/nptyping/__init__.py index 8641108..4ca8fdb 100644 --- a/src/numpydantic/vendor/nptyping/__init__.py +++ b/src/numpydantic/vendor/nptyping/__init__.py @@ -42,10 +42,8 @@ from numpydantic.vendor.nptyping.shape_expression import ( from numpydantic.vendor.nptyping.structure import Structure from numpydantic.vendor.nptyping.typing_ import ( Bool, - Bool8, Byte, Bytes, - Bytes0, CDouble, CFloat, Character, @@ -68,7 +66,6 @@ from numpydantic.vendor.nptyping.typing_ import ( Half, Inexact, Int, - Int0, Int8, Int16, Int32, @@ -82,17 +79,14 @@ from numpydantic.vendor.nptyping.typing_ import ( LongLong, Number, Object, - Object0, Short, SignedInteger, Single, SingleComplex, - Str0, String, Timedelta64, UByte, UInt, - UInt0, UInt8, UInt16, UInt32, @@ -104,7 +98,6 @@ from numpydantic.vendor.nptyping.typing_ import ( UnsignedInteger, UShort, Void, - Void0, ) __all__ = [ diff --git a/src/numpydantic/vendor/nptyping/typing_.py b/src/numpydantic/vendor/nptyping/typing_.py index 6b05eb6..6caf3b9 100644 --- a/src/numpydantic/vendor/nptyping/typing_.py +++ b/src/numpydantic/vendor/nptyping/typing_.py @@ -22,11 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from typing import ( # type: ignore[attr-defined,misc] # pylint: disable=unused-import - Tuple, - TypeAlias, - Union, -) +import sys +from typing import Tuple, Union + +if sys.version_info.minor >= 10: + from typing import TypeAlias +else: + from typing_extensions import TypeAlias import numpy as np diff --git a/src/numpydantic/vendor/nptyping/typing_.pyi b/src/numpydantic/vendor/nptyping/typing_.pyi deleted file mode 100644 index fcf83ce..0000000 --- a/src/numpydantic/vendor/nptyping/typing_.pyi +++ /dev/null @@ -1,114 +0,0 @@ -""" -MIT License - -Copyright (c) 2023 Ramon Hagenaars - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -""" - -try: - from typing import ( # type: ignore[attr-defined] # pylint: disable=unused-import - Dict, - TypeAlias, - ) -except ImportError: # pragma: no cover - from typing_extensions import ( - TypeAlias, - ) - -from typing import ( - Any, - Tuple, - Union, -) - -import numpy as np - -ShapeExpression: TypeAlias = str -StructureExpression: TypeAlias = str -DType: TypeAlias = Union[np.generic, StructureExpression] -ShapeTuple: TypeAlias = Tuple[int, ...] - -Number: TypeAlias = np.dtype[np.number[Any]] -Bool: TypeAlias = np.dtype[np.bool_] -Bool8: TypeAlias = np.dtype[np.bool8] -Object: TypeAlias = np.dtype[np.object_] -Object0: TypeAlias = np.dtype[np.object0] -Datetime64: TypeAlias = np.dtype[np.datetime64] -Integer: TypeAlias = np.dtype[np.integer[Any]] -SignedInteger: TypeAlias = np.dtype[np.signedinteger[Any]] -Int8: TypeAlias = np.dtype[np.int8] -Int16: TypeAlias = np.dtype[np.int16] -Int32: TypeAlias = np.dtype[np.int32] -Int64: TypeAlias = np.dtype[np.int64] -Byte: TypeAlias = np.dtype[np.byte] -Short: TypeAlias = np.dtype[np.short] -IntC: TypeAlias = np.dtype[np.intc] -IntP: TypeAlias = np.dtype[np.intp] -Int0: TypeAlias = np.dtype[np.int0] -Int: TypeAlias = np.dtype[np.int_] -LongLong: TypeAlias = np.dtype[np.longlong] -Timedelta64: TypeAlias = np.dtype[np.timedelta64] -UnsignedInteger: TypeAlias = np.dtype[np.unsignedinteger[Any]] -UInt8: TypeAlias = np.dtype[np.uint8] -UInt16: TypeAlias = np.dtype[np.uint16] -UInt32: TypeAlias = np.dtype[np.uint32] -UInt64: TypeAlias = np.dtype[np.uint64] -UByte: TypeAlias = np.dtype[np.ubyte] -UShort: TypeAlias = np.dtype[np.ushort] -UIntC: TypeAlias = np.dtype[np.uintc] -UIntP: TypeAlias = np.dtype[np.uintp] -UInt0: TypeAlias = np.dtype[np.uint0] -UInt: TypeAlias = np.dtype[np.uint] -ULongLong: TypeAlias = np.dtype[np.ulonglong] -Inexact: TypeAlias = np.dtype[np.inexact[Any]] -Floating: TypeAlias = np.dtype[np.floating[Any]] -Float16: TypeAlias = np.dtype[np.float16] -Float32: TypeAlias = np.dtype[np.float32] -Float64: TypeAlias = np.dtype[np.float64] -Half: TypeAlias = np.dtype[np.half] -Single: TypeAlias = np.dtype[np.single] -Double: TypeAlias = np.dtype[np.double] -Float: TypeAlias = np.dtype[np.float_] -LongDouble: TypeAlias = np.dtype[np.longdouble] -LongFloat: TypeAlias = np.dtype[np.longfloat] -ComplexFloating: TypeAlias = np.dtype[np.complexfloating[Any, Any]] -Complex64: TypeAlias = np.dtype[np.complex64] -Complex128: TypeAlias = np.dtype[np.complex128] -CSingle: TypeAlias = np.dtype[np.csingle] -SingleComplex: TypeAlias = np.dtype[np.singlecomplex] -CDouble: TypeAlias = np.dtype[np.cdouble] -Complex: TypeAlias = np.dtype[np.complex_] -CFloat: TypeAlias = np.dtype[np.cfloat] -CLongDouble: TypeAlias = np.dtype[np.clongdouble] -CLongFloat: TypeAlias = np.dtype[np.clongfloat] -LongComplex: TypeAlias = np.dtype[np.longcomplex] -Flexible: TypeAlias = np.dtype[np.flexible] -Void: TypeAlias = np.dtype[np.void] -Void0: TypeAlias = np.dtype[np.void0] -Character: TypeAlias = np.dtype[np.character] -Bytes: TypeAlias = np.dtype[np.bytes_] -Str: TypeAlias = np.dtype[np.str_] -String: TypeAlias = np.dtype[np.string_] -Bytes0: TypeAlias = np.dtype[np.bytes0] -Unicode: TypeAlias = np.dtype[np.unicode_] -Str0: TypeAlias = np.dtype[np.str0] - -dtype_per_name: Dict[str, np.dtype[Any]] -name_per_dtype: Dict[np.dtype[Any], str] diff --git a/tests/test_ndarray.py b/tests/test_ndarray.py index 9e45c1e..62ae367 100644 --- a/tests/test_ndarray.py +++ b/tests/test_ndarray.py @@ -7,11 +7,12 @@ import json import numpy as np from pydantic import BaseModel, ValidationError, Field -from nptyping import Number + from numpydantic import NDArray, Shape from numpydantic.exceptions import ShapeError, DtypeError from numpydantic import dtype +from numpydantic.dtype import Number def test_ndarray_type(): diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..9f1fefc --- /dev/null +++ b/tox.ini @@ -0,0 +1,14 @@ +[tox] +requires = + tox>=4 +env_list = py{39,310,311,312}-numpy{1,2} + +[testenv] +package = editable +extras = + tests +deps = + py{39,310,311,312}-numpy1: numpy<2.0.0 + py{39,310,311,312}-numpy2: numpy>=2.0.0 +commands = + python -m pytest