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

Commit

Permalink
Added Warning and Exception annotations (#57)
Browse files Browse the repository at this point in the history
See https://github.com/numpy/numpy-stubs/issues/54.

Added annotations and tests for:
* np.ModuleDeprecationWarning
* np.VisibleDeprecationWarning
* np.ComplexWarning
* np.RankWarning
* np.TooHardError
* np.AxisError
  • Loading branch information
BvB93 committed Apr 18, 2020
1 parent 402eb84 commit 559ff04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
16 changes: 15 additions & 1 deletion numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class dtype:
def descr(self) -> List[Union[Tuple[str, str], Tuple[str, str, _Shape]]]: ...
@property
def fields(
self
self,
) -> Optional[Mapping[str, Union[Tuple[dtype, int], Tuple[dtype, int, Any]]]]: ...
@property
def flags(self) -> int: ...
Expand Down Expand Up @@ -778,3 +778,17 @@ trunc: ufunc

# TODO(shoyer): remove when the full numpy namespace is defined
def __getattr__(name: str) -> Any: ...

# Warnings
class ModuleDeprecationWarning(DeprecationWarning): ...
class VisibleDeprecationWarning(UserWarning): ...
class ComplexWarning(RuntimeWarning): ...
class RankWarning(UserWarning): ...

# Errors
class TooHardError(RuntimeError): ...

class AxisError(ValueError, IndexError):
def __init__(
self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ...
) -> None: ...
5 changes: 5 additions & 0 deletions tests/fail/warnings_and_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import numpy as np

np.AxisError(1.0) # E: Argument 1 to "AxisError" has incompatible type
np.AxisError(1, ndim=2.0) # E: Argument "ndim" to "AxisError" has incompatible type
np.AxisError(2, msg_prefix=404) # E: Argument "msg_prefix" to "AxisError" has incompatible type
7 changes: 7 additions & 0 deletions tests/pass/warnings_and_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import numpy as np

np.AxisError(1)
np.AxisError(1, ndim=2)
np.AxisError(1, ndim=None)
np.AxisError(1, ndim=2, msg_prefix='error')
np.AxisError(1, ndim=2, msg_prefix=None)
10 changes: 10 additions & 0 deletions tests/reveal/warnings_and_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Type

import numpy as np

reveal_type(np.ModuleDeprecationWarning()) # E: numpy.ModuleDeprecationWarning
reveal_type(np.VisibleDeprecationWarning()) # E: numpy.VisibleDeprecationWarning
reveal_type(np.ComplexWarning()) # E: numpy.ComplexWarning
reveal_type(np.RankWarning()) # E: numpy.RankWarning
reveal_type(np.TooHardError()) # E: numpy.TooHardError
reveal_type(np.AxisError(1)) # E: numpy.AxisError

0 comments on commit 559ff04

Please sign in to comment.