ramonhagenaars/nptyping

Why not support a "by convention" use of nptyping?

denisrosset opened this issue · 3 comments

For the people I work with, Pylance/Pyright support is important.

Would it be possible to use nptyping Shape annotations with Annotated, and document that possibility? note: that wouldn't require even importing the nptyping.

This could be done through the convention of having a string annotation in Annotated, or a dataclass wrapping a string.

Then the downstream tools such as beartype and typeguard could be modified easily to support this syntax, and other type checkers/code analyzers would simply ignore the additional information.

What do you think?

You could use typing.Annotated as an alternative to nptyping, yes. Nothing will stop you from adopting the shape expression syntax:

from typing import Annotated
import numpy as np

Array2x3 = Annotated[np.ndarray, "2, [a, b, c]"]

def func(arr: Array2x3) -> None:
    ...

Having beartype and typeguard to do the instance checking is harder though. Because that would require beartype and typeguard to either implement the shape expression or depend on nptyping. Unless they offer some sort of plug-in or hooking mechanism, maybe @leycec from beartype knows?

some sort of plug-in or hooking mechanism

You ask for much, misty mountain gorilla. I actually have a FIXME: comment to myself buried somewhere within the @beartype codebase begging myself to publicly expose a plugin API in a future release. After all, @beartype currently handles non-standard numpy.typing.NDArray[...] type hints by just internally reducing them to standard typing.Annotated[numpy.ndarray, beartype.vale.Is*[...]] type hints. So, we should let other typing enthusiasts (e.g., everyone here) do that too. 👍

Sadly, I myself am currently the plugin and hooking mechanism. Ask – and ye shall probably eventually receive! However...

Annotated[np.ndarray, "2, [a, b, c]"]

...that's pretty sketchy, bro. Ain't nobody dynamically evaluating, parsing, or otherwise munging ad-hoc strings embedded in type hints. On the other hand...

from nptyping import NDArray, Shape

# Perhaps this...
Annotated[np.ndarray, Shape['2, 2']]

# ...or maybe this!
Annotated[np.ndarray, NDArray[Shape['2, 2']]]

...are both much more sensible. I don't know quite enough about nptyping internals to know which @ramonhagenaars would prefer we support, however. Neither? Both? I leave the delicate decision in the capable sledgehammer-sized hands of the simian.

Indeed, simply tacking on a string is unwise.