10000 MAINT: move things in `numeric.pyi` to the top-level `__init__.pyi` by person142 · Pull Request #50 · numpy/numpy-stubs · GitHub
[go: up one dir, main page]

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

MAINT: move things in numeric.pyi to the top-level __init__.pyi #50

Merged
merged 1 commit into from
Apr 14, 2020

Conversation

person142
Copy link
Member

In gh-38 some stubs were added for core/numeric.py. The problem is
that these stubs are not picked up at all, as can be tested with

import numpy as np

reveal_type(np.ones_like)

which on master gives

Revealed type is 'Any'

The issue is that the NumPy main repo importing from numeric doesn't
automatically mean the type stubs do. We could add the relevant
imports on the stubs level too, but there is no need for the structure
of the stubs to mimic the structure of NumPy unless they are being
used to type-check NumPy itself (which we aren't doing yet), and
indeed having them mimic the internal structure just causes extra pain
of trying to keep up with internal refactors that don't affect the
public API. In fact, we already ignore NumPy's internal structure by
defining ndarray in the top-level __init__.pyi, when it is
actually exported by numeric.

So instead, just fix the issue by moving the numeric stubs into the
top level __init__.pyi (plus some minor bug fixes). On master the
above script now correctly gives:

Revealed type is 'def [_ArrayLike] (a: _ArrayLike`-1, dtype: Union[numpy.dtype, None] =, order: builtins.str =, subok: builtins.bool =, shape: Union[builtins.int, typing.Sequence[builtins.int], None] =) -> numpy.ndarray

@person142
Copy link
Member Author

Note that these functions still have no tests (which is how this didn't get caught originally); I didn't try to fix that in this PR.

) -> _ArrayLike: ...
def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
def extend_all(module: ModuleType): ...
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't a public function, so I removed it.

axis: Optional[Union[int, Tuple[int, ...]]] = ...,
) -> _T: ...
def rollaxis(a: _ArrayLike, axis: int, start: int = ...) -> ndarray: ...
def normalize_axis_tuple(
Copy link
Member Author

Choose a reason for hiding this comment

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

Not a public function; removed it.

subok: bool = ...,
shape: Optional[Union[int, Sequence[int]]] = ...,
) -> ndarray[int]: ...
def ones(
Copy link
Member Author

Choose a reason for hiding this comment

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

Already defined in __init__.pyi.

order: str = ...,
subok: bool = ...,
shape: Optional[Union[int, Sequence[int]]] = ...,
) -> ndarray[int]: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

Converted the various ndarray[T] things to ndarray, since that's not a thing yet (#48).

@rgommers
Copy link
Member

So instead, just fix the issue by moving the numeric stubs into the
top level __init__.pyi (plus some minor bug fixes).

makes sense to me

In numpygh-38 some stubs were added for `core/numeric.py`. The problem is
that these stubs are not picked up at all, as can be tested with

```python
import numpy as np

reveal_type(np.ones_like)
```

which on master gives

```
Revealed type is 'Any'
```

The issue is that the NumPy main repo importing from numeric doesn't
automatically mean the type stubs do. We could add the relevant
imports on the stubs level too, but there is no need for the structure
of the stubs to mimic the structure of NumPy unless they are being
used to type-check NumPy itself (which we aren't doing yet), and
indeed having them mimic the internal structure just causes extra pain
of trying to keep up with internal refactors that don't affect the
public API. In fact, we already ignore NumPy's internal structure by
defining `ndarray` in the top-level `__init__.pyi`, when it is
actually exported by `numeric.py`.

So instead, just fix the issue by moving the numeric stubs into the
top level `__init__.pyi` (plus some minor bug fixes). On master the
above script now correctly gives:

```
Revealed type is 'def [_ArrayLike] (a: _ArrayLike`-1, dtype: Union[numpy.dtype, None] =, order: builtins.str =, subok: builtins.bool =, shape: Union[builtins.int, typing.Sequence[builtins.int], None] =) -> numpy.ndarray
````
@person142
Copy link
Member Author

Ok, fixed the pyflakes error.

@rgommers rgommers merged commit f335f16 into numpy:master Apr 14, 2020
@rgommers
Copy link
Member

LGTM, merged. Thanks @person142

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0