Replies: 3 comments 7 replies
-
cc: @BvB93 |
Beta Was this translation helpful? Give feedback.
-
I agree that it might be nice to have something like this at some point. Interstingly, if the dtype objects are proper classes (or at least treated as such when type checking) then there is no need to distinguish between the object and the annotation, i.e. this would allow the likes of from typing import TYPE_CHECKING
import numpy as np
# For example; the `TYPE_CHECKING` may or may not be necessary depending on the particular implementation
if TYPE_CHECKING:
class float64(np.dtype[np.float64]): ...
else:
float64 = np.dtype(np.float64)
foo_dtype: float64
I would love to see
This will likelly be quite challenging, as there is no convenient way of typing namespaces (e.g. via
The aliases (sort of) already exist with the numeric tower from PEP 484, namely: |
Beta Was this translation helpful? Give feedback.
-
Just came across this older thread. I don't know what kind of interest this topic has at the moment but I thought I'd point folks at the TorchTyping package and the newly-released jaxtyping package for interest. |
Beta Was this translation helpful? Give feedback.
-
Whilst I've been working on Array API support for Hypothesis and the test suite, I've been interested in type hinting some functions that take/return Array API objects (i.e. data type objects and arrays). I think it would be cool if there was a package to do this "properly".
How I picture it right now:
Bool
.UInt64
Int64
,Float64
(these are type hinting the data type objects e.g.xp.bool
,xp.int64
andxp.float64
, but I'm not sure I like using something likeBoolType
as it's a bit tiresome IMO)Int[64]
andFloat[64]
...?Integer
,SignedInteger
,UnsignedInteger
,Floating
DataType
- any data type objectsArray
, a generic which can take data type hints likeArray[Int64]
orArray[Floating]
isinstance()
by way of checking__array_namespace__
, which notably we suggest in the spec for general conformance checkingArrayModule
for thexp
namespaceiinfo
andfinfo
Scalar
as an alias ofUnion[bool, int, float]
(in the future alsocomplex
)RealScalar
as an alias ofUnion[int, float]
ComplexScalar
as an alias ofUnion[int, float, complex]
Obviously the utility of type hints for actual type checking are pretty limited (save checking a generic
Array
andArrayModule
), unless I'm missing some easy way to grab the array modulexp
so that something likeBool
could useobj == xp.bool
. Maybe there could be amake_types_namespace
-ish to boundxp
so these type hints can actually help mypy lolThat said I like using them just for a scrappy documentation myself, and I wonder if that could be the case for folks developing array-consuming methods for the Array API. I think a generic
Array
type could be interesting for writing spec, as we could annotate input and output arrays with their categorial type hintsAlso a niche use case—such type hints could maybe be used in Hypothesis for inferring strategies via type hints.
Anywho, interested if folks have ideas for what a good API for such a package would look like. Thought to myself I'd like to work on this as a fun side project in the not-so-near future (I have 0 idea how to make stuff like generic type hints so thought it'd be a good excuse to learn haha).
Beta Was this translation helpful? Give feedback.
All reactions