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

Add basic stubs for some class attributes #9

Merged
merged 10 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.mypy_cache
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: python
python: 3.6

notifications:
email: false

install:
- pip install -r test-requirements.txt
- if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then pip install flake8-pyi==17.3.0; fi

script:
- flake8
- MYPYPATH="." mypy tests/test_simple.py

cache:
directories:
- "$HOME/.cache/pip"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# numpy_stubs: experimental typing stubs for NumPy

[![Build Status](https://travis-ci.org/numpy/numpy_stubs.svg?branch=master)](https://travis-ci.org/numpy/numpy_stubs)

This repository exists for developing [PEP 484](https://www.python.org/dev/peps/pep-0484/)
compatible typing annotations for [NumPy](https://github.com/numpy/numpy).

Expand Down
184 changes: 182 additions & 2 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,188 @@
# very simple, just enough to start running tests
#
import builtins
from typing import Any, Mapping, List, Optional, Tuple, Union

class ndarray: pass
from numpy.core._internal import _ctypes

_Shape = Tuple[int, ...]

class dtype:
names: Optional[Tuple[str, ...]]

@property
def alignment(self) -> int: ...

@property
def base(self) -> dtype: ...

@property
def byteorder(self) -> str: ...

@property
def char(self) -> str: ...

@property
def descr(self) -> List[Union[
Tuple[str, str],
Tuple[str, str, _Shape]]]: ...

@property
def fields(self) -> Optional[Mapping[
str,
Union[Tuple[dtype, int],
Tuple[dtype, int, Any]]]]: ...

@property
def flags(self) -> int: ...

@property
def hasobject(self) -> bool: ...

@property
def isbuiltin(self) -> int: ...

@property
def isnative(self) -> bool: ...

@property
def isalignedstruct(self) -> bool: ...

@property
def itemsize(self) -> int: ...

@property
def kind(self) -> str: ...

@property
def metadata(self) -> Optional[Mapping[str, Any]]: ...

@property
def name(self) -> str: ...

@property
def num(self) -> int: ...

@property
def shape(self) -> _Shape: ...

@property
def ndim(self) -> int: ...

@property
def subdtype(self) -> Optional[Tuple[dtype, _Shape]]: ...

def newbyteorder(self, new_order: str = ...) -> dtype: ...

# Leave str and type for end to avoid having to use `builtins.str`
# everywhere. See https://github.com/python/mypy/issues/3775
@property
def str(self) -> builtins.str: ...

@property
def type(self) -> builtins.type: ...
Copy link
Member

@shoyer shoyer Feb 19, 2018

Choose a reason for hiding this comment

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

More specifically, we could define the np.generic class and switch this to Type[generic] (but this is also fine for now -- certainly let's save defining the full NumPy scalar type hierarchy for later).



_dtype_class = dtype # for ndarray type
Copy link
Member

Choose a reason for hiding this comment

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

Why does this have a duplicate name?

Copy link
Member

Choose a reason for hiding this comment

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

Otherwise mypy gets confused when you to type the dtype ndarray attribute, e.g., def dtype(self) -> dtype refers to ndarray.dtype for the return value, not the dtype class.

Copy link
Member

Choose a reason for hiding this comment

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

Can you fix it with an import numpy as np, and then using np.dtype?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know exactly how mypy handles circular imports, but I suspect that importing a module from itself would not work.



class _flagsobj:
aligned: bool
updateifcopy: bool
writeable: bool
writebackifcopy: bool

@property
def behaved(self) -> bool: ...

@property
def c_contiguous(self) -> bool: ...

@property
def carray(self) -> bool: ...

@property
def contiguous(self) -> bool: ...

@property
def f_contiguous(self) -> bool: ...

@property
def farray(self) -> bool: ...

@property
def fnc(self) -> bool: ...

@property
def forc(self) -> bool: ...

@property
def fortran(self) -> bool: ...

@property
def num(self) -> int: ...

@property
def owndata(self) -> bool: ...

def __getitem__(self, key: str) -> bool: ...
def __setitem__(self, key: str, value: bool) -> None: ...


class flatiter:
@property
def base(self) -> ndarray: ...

@property
def coords(self) -> _Shape: ...

@property
def index(self) -> int: ...

def copy(self) -> ndarray: ...
def __iter__(self) -> flatiter: ...
def __next__(self) -> Any: ...


class ndarray:
dtype: _dtype_class
imag: ndarray
real: ndarray
shape: _Shape
strides: Tuple[int, ...]

@property
def T(self) -> ndarray: ...

@property
def base(self) -> Optional[ndarray]: ...

@property
def ctypes(self) -> _ctypes: ...

@property
def data(self) -> memoryview: ...

@property
def flags(self) -> _flagsobj: ...

@property
def flat(self) -> flatiter: ...

@property
def size(self) -> int: ...

@property
def itemsize(self) -> int: ...

@property
def nbytes(self) -> int: ...

@property
def ndim(self) -> int: ...

def __getattr__(self, name) -> Any: ...

class dtype: pass

def array(
object: object,
Expand Down
Empty file added numpy/core/__init__.pyi
Empty file.
21 changes: 21 additions & 0 deletions numpy/core/_internal.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any

# TODO: add better annotations when ctypes is stubbed out

class _ctypes:
@property
def data(self) -> int: ...

@property
def shape(self) -> Any: ...

@property
def strides(self) -> Any: ...

def data_as(self, obj: Any) -> Any: ...
def shape_as(self, obj: Any) -> Any: ...
def strides_as(self, obj: Any) -> Any: ...
def get_data(self) -> int: ...
def get_shape(self) -> Any: ...
def get_strides(self) -> Any: ...
def get_as_parameter(self) -> Any: ...
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore the following errors (for stubs):
# E301 expected 1 blank line, found 0
# E302 expected 2 blank lines, found 1
# E305 expected 2 blank lines after class or function definition, found 1
# E701 multiple statements on one line (colon)
# E704 multiple statements on one line (def)

[flake8]
ignore = E301, E302, E305, E701, E704
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

setup(
name='numpy_stubs',
name='numpy-stubs',
maintainer="NumPy Developers",
maintainer_email="numpy-discussion@python.org",
description="PEP 561 type stubs for numpy",
Expand All @@ -11,8 +11,8 @@
packages=find_packages(),

# PEP 561 requires these
install_requires=['numpy~=1.13.0'],
install_requires=['numpy~=1.14.0'],
package_data={
'numpy': 'py.typed'
'numpy': 'py.typed'
},
)
3 changes: 3 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flake8==3.3.0

mypy==0.560.0