Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Add array conversion methods to ndarray #21

Merged
merged 5 commits into from Nov 19, 2018
Merged

Add array conversion methods to ndarray #21

merged 5 commits into from Nov 19, 2018

Conversation

sproshev
Copy link
Contributor

@sproshev sproshev commented Nov 12, 2018

Actual types are like:

def item(self, *args: int) -> T: ...
@overload
def itemset(self, __value: T): ...
@overload
def itemset(self, __item: _ShapeLike, __value: T): ...
def astype(self,
           dtype: _DtypeLike,
           order: str=...,
           casting: str=...,
           subok: bool=...,
           copy: bool=...) -> ndarray[dtype]: ...
def byteswap(self, inplace: bool=...) -> ndarray[T]: ...
def copy(self, order: str=...) -> ndarray[T]: ...
def view(self, dtype: Union[_DtypeLike, NdArrayOrMatrix]=..., type: NdArrayOrMatrix=...) -> NdArrayOrMatrix[dtype]: ...
def getfield(self, dtype: Union[_DtypeLike, str], offset: int=...) -> ndarray[dtype]: ...

Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these PRs! I'll just point out that we also have tests setup that might be worth augmenting for any complex cases: https://github.com/numpy/numpy-stubs/tree/master/tests

def copy(self, order: str=...) -> ndarray: ...
def view(self,
dtype: Union[_DtypeLike, Type]=...,
type: Any=...) -> ndarray: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably substitute Type -> Type[ndarray] and Any -> Type[ndarray].

Copy link
Contributor Author

@sproshev sproshev Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Fixed. Covered with tests

Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but just to be clear you don't need tests for everything -- especially not for simple methods like dumps() that just returns bytes.

I would save tests for complex cases where it's not immediately evident that the code is correct, e.g., where you have overloads or type variables.

reveal_type(nd.view(np.int64)) # E: numpy.ndarray
reveal_type(nd.view(dtype=np.int64)) # E: numpy.ndarray
reveal_type(nd.view(np.int64, np.matrix)) # E: numpy.ndarray
reveal_type(nd.view(type=np.matrix)) # E: numpy.ndarray
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, I think this should be numpy.matrix.

Or we can could make a dummy subclass, if np.matrix isn't defined, e.g.,

class SubArray(np.ndarray):
    pass

reveal_type(nd.view(type=SubArray))  # E: SubArray

Does that actually work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for review! I've removed simple tests and fixed type hints for view.

Remove simple ones and leave comments for consistency
Its stub was splitted into all possible overloads.
`view(self, dtype: Type[_NdArraySubClass], type: Type[_NdArraySubClass])` could not be used.
@shoyer shoyer merged commit f1a613b into numpy:master Nov 19, 2018
@sproshev sproshev deleted the sproshev/array-conversion branch November 20, 2018 16:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants