8000 Add basic stubs for some class attributes by alanhdu · Pull Request #9 · 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.

Add basic stubs for some class attributes #9

Merged
merged 10 commits into from
Feb 19, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix some type annotations
  • Loading branch information
alanhdu committed Feb 18, 2018
commit 59bd6bcc852a77994090fb67e9c9451e629242d8
14 changes: 7 additions & 7 deletions numpy/__init__.pyi
8000
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class dtype:
alignment: int
byteorder: str
char: str
descr: List[Tuple[str, str]]
descr: List[Union[Tuple[str, str], Tuple[str, str, Tuple[int, ...]]]]
fields: Optional[Mapping[str, Union[Tuple[dtype, int], Tuple[dtype, int, str]]]]
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the final title field can have any type, so it should probably be keyed as Any instead of str: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.dtype.fields.html#numpy.dtype.fields

flags: int
hasobject: bool
Expand All @@ -26,7 +26,7 @@ class dtype:
subdtype: Optional[Tuple[dtype, Tuple[int, ...]]]

Copy link
Member

Choose a reason for hiding this comment

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

It isn't documented, but metadata is also used out in the wild (e.g., by h5py). It should be Optional[Mapping].


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

str: builtins.str
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've segregated this here so that the other strs don't need to have builtins.str here (see python/mypy#3775).

Copy link
Member

Choose a reason for hiding this comment

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

Please add this as a comment in the code

type: builtins.type
Expand All @@ -35,10 +35,10 @@ class dtype:
def base(self) -> dtype: ...
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why this is a property vs. an attribute for everything else? Does mypy care about the difference?

Choose a reason for hiding this comment

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

Properties can't be assigned to unless they have a setter. With this stub, if you had x: dtype, mypy would allow x.type = some_type but not x.base = some_dtype.

Copy link
Member

Choose a reason for hiding this comment

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

OK, in that case we should also convert most of the other attributes into readonly properties.



_dtype = dtype # for ndarray type
_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:
class _flagsobj:
aligned: bool
behaved: bool
c_contiguous: bool
Expand Down Expand Up @@ -72,9 +72,9 @@ class flatiter:
class ndarray:
T: ndarray
data: memoryview
dtype: _dtype
flags: flagsobj
flat: Any
dtype: _dtype_class
flags: _flagsobj
flat: flatiter
imag: ndarray
real: ndarray
size: int
Expand Down
0