Now provides PyTreeDef, and can detect PyTrees via issubclass(x, PyTree)

This commit is contained in:
Patrick Kidger
2023-06-01 10:56:00 -07:00
parent 10e1852b37
commit 6a64ef114e
7 changed files with 68 additions and 43 deletions
+10 -5
View File
@@ -1,13 +1,18 @@
# Advanced features
## Abstract base classes
## Creating your own dtypes
::: jaxtyping.AbstractDtype
selection:
members:
false
::: jaxtyping.AbstractArray
selection:
members:
false
## Introspection
If you're writing your own type hint parser, then you may wish to detect if some Python object is a jaxtyping-provided type.
You can check for dtypes by doing `issubclass(x, AbstractDtype)`. For example, `issubclass(Float32, AbstractDtype)` will pass.
You can check for arrays by doing `issubclass(x, AbstractArray)`. Here, `AbstractArray` is the base class for all shape-and-dtype specified arrays, e.g. it's a base class for `Float32[Array, "foo"]`.
You can check for pytrees by doing `issubclass(x, PyTree)`. For example, `issubclass(PyTree[int], PyTree)` will pass.
+5 -1
View File
@@ -5,4 +5,8 @@
members:
false
Note that `jaxtyping.PyTree` is only available if JAX has been installed.
`jaxtyping.PyTreeDef` is an alias for `jax.tree_util.PyTreeDef`, which is the type of the return from `jax.tree_util.tree_structure(...)`.
:::jaxtyping.PyTreeDef
Note that `jaxtyping.{PyTree, PyTreeDef}` are only available if JAX has been installed.