ramonhagenaars/nptyping

Why does NDArray[Any] not work anymore?

Closed this issue · 3 comments

[/usr/local/lib/python3.7/dist-packages/pyterrier/apply.py](https://localhost:8080/#) in <module>()
     79     return ApplyDocumentScoringTransformer(fn, *args, **kwargs)
     80 
---> 81 def doc_features(fn : Callable[..., NDArray[Any]], *args, **kwargs) -> Transformer:
     82     """
     83         Create a transformer that takes as input a ranked documents dataframe, and applies the supplied function to each document to compute feature scores.

[/usr/local/lib/python3.7/dist-packages/nptyping/ndarray.py](https://localhost:8080/#) in __getitem__(cls, item)
     70             raise NPTypingError(f"Type {cls} is already parameterized")
     71         if not isinstance(item, tuple):
---> 72             raise InvalidArgumentsError(f"Unexpected argument of type {type(item)}")
     73         shape_expression, dtype = _get_from_tuple(item)
     74         validate_dtype(dtype)

InvalidArgumentsError: Unexpected argument of type <class 'typing._SpecialForm'>

That code worked fine on previous nptyping

The interface of nptyping.NDArray has changed per 2.0.0 into NDArray[<shape expression, <dtype>].

There now are 4 ways to annotate an "any array":

# Option 1:
arr1: np.ndarray  # Not MyPy-friendly

# Option 2:
arr2: NDArray  # Not MyPy-friendly

# Option 3:
arr3: NDArray[Any, Any]

# Option 4:
arr4: NDArray[Shape["*, ..."], Any]

I'd recommend to use option 3 or 4.

Perhaps a warning in the last 1.x release would have given a warning rather than breaking dependent software.

nptyping follows SemVer: major version upgrades are breaking. There have been 2 alpha releases before the final 2.0.0 release.

Make sure your requirements are always pinned to a range, e.g. pip install "nptyping>=1.0.0,<2.0.0". See also PEP-0440