From f482f07be9529b022415a83d2c636c446c14ec0e Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Fri, 14 Jun 2024 22:56:28 -0700 Subject: [PATCH] 1.2.0 - shape ranges update docs, bump version --- docs/api/shape.md | 7 +++++++ docs/changelog.md | 4 ++++ docs/index.md | 1 + docs/syntax.md | 18 ++++++++++++++++++ pyproject.toml | 2 +- src/numpydantic/shape.py | 8 ++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 docs/api/shape.md diff --git a/docs/api/shape.md b/docs/api/shape.md new file mode 100644 index 0000000..4c8638c --- /dev/null +++ b/docs/api/shape.md @@ -0,0 +1,7 @@ +# shape + +```{eval-rst} +.. automodule:: numpydantic.shape + :members: + :undoc-members: +``` \ No newline at end of file diff --git a/docs/changelog.md b/docs/changelog.md index be89fe1..41c0a7c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,10 @@ ## 1.* +### 1.2.0 - 24-06-13 - Shape ranges + +- Add ability to specify shapes as ranges - see [shape ranges](shape-ranges) + ### 1.1.0 - 24-05-24 - Instance Checking https://github.com/p2p-ld/numpydantic/pull/1 diff --git a/docs/index.md b/docs/index.md index 5c2b680..af2c908 100644 --- a/docs/index.md +++ b/docs/index.md @@ -491,6 +491,7 @@ api/maps api/meta api/monkeypatch api/schema +api/shape api/types ``` diff --git a/docs/syntax.md b/docs/syntax.md index 074fa24..bfd25fa 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -61,6 +61,24 @@ For a 2-dimensional, 3 x any-shaped array: Shape["3, *"] ``` +(shape-ranges)= +### Ranges + +Dimension sizes can also be specified as ranges[^ranges]. +Ranges must have no whitespace, and may use integers or wildcards. +Range specifications are **inclusive** on both ends. + +For an array whose... +- First dimension can be of length 2, 3, or 4 +- Second dimension is 2 or greater +- Third dimension is 4 or less + +```python +Shape["2-4, 2-*, *-4"] +``` + +[^ranges]: This is an extension to nptyping's syntax, and so using `nptyping.Shape` is unsupported - use {class}`numpydantic.Shape` + ### Labels Dimensions can be given labels, and in future versions these labels will be diff --git a/pyproject.toml b/pyproject.toml index 9136e37..ecb57ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "numpydantic" -version = "1.1.0" +version = "1.2.0" description = "Type and shape validation and serialization for numpy arrays in pydantic models" authors = [ {name = "sneakers-the-rat", email = "sneakers-the-rat@protonmail.com"}, diff --git a/src/numpydantic/shape.py b/src/numpydantic/shape.py index 3b82149..c739e49 100644 --- a/src/numpydantic/shape.py +++ b/src/numpydantic/shape.py @@ -58,6 +58,14 @@ class ShapeMeta(ContainerMeta, implementation="Shape"): dim_string_without_labels = remove_labels(dim_strings) return {"prepared_args": dim_string_without_labels} + def __setattr__(cls, key: str, value: Any) -> None: # pragma: no cover + """just for documentation generation - allow __annotations__""" + + if key not in ("_abc_impl", "__abstractmethods__", "__annotations__"): + raise NPTypingError(f"Cannot set values to nptyping.{cls.__name__}.") + else: + object.__setattr__(cls, key, value) + class Shape(NPTypingType, ABC, metaclass=ShapeMeta): """