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

Commit 559ff04

Browse files
authored
Added Warning and Exception annotations (#57)
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
1 parent 402eb84 commit 559ff04

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

numpy-stubs/__init__.pyi

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class dtype:
103103
def descr(self) -> List[Union[Tuple[str, str], Tuple[str, str, _Shape]]]: ...
104104
@property
105105
def fields(
106-
self
106+
self,
107107
) -> Optional[Mapping[str, Union[Tuple[dtype, int], Tuple[dtype, int, Any]]]]: ...
108108
@property
109109
def flags(self) -> int: ...
@@ -778,3 +778,17 @@ trunc: ufunc
778778

779779
# TODO(shoyer): remove when the full numpy namespace is defined
780780
def __getattr__(name: str) -> Any: ...
781+
782+
# Warnings
783+
class ModuleDeprecationWarning(DeprecationWarning): ...
784+
class VisibleDeprecationWarning(UserWarning): ...
785+
class ComplexWarning(RuntimeWarning): ...
786+
class RankWarning(UserWarning): ...
787+
788+
# Errors
789+
class TooHardError(RuntimeError): ...
790+
791+
class AxisError(ValueError, IndexError):
792+
def __init__(
793+
self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ...
794+
) -> None: ...

tests/fail/warnings_and_errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import numpy as np
2+
3+
np.AxisError(1.0) # E: Argument 1 to "AxisError" has incompatible type
4+
np.AxisError(1, ndim=2.0) # E: Argument "ndim" to "AxisError" has incompatible type
5+
np.AxisError(2, msg_prefix=404) # E: Argument "msg_prefix" to "AxisError" has incompatible type

tests/pass/warnings_and_errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import numpy as np
2+
3+
np.AxisError(1)
4+
np.AxisError(1, ndim=2)
5+
np.AxisError(1, ndim=None)
6+
np.AxisError(1, ndim=2, msg_prefix='error')
7+
np.AxisError(1, ndim=2, msg_prefix=None)

tests/reveal/warnings_and_errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Type
2+
3+
import numpy as np
4+
5+
reveal_type(np.ModuleDeprecationWarning()) # E: numpy.ModuleDeprecationWarning
6+
reveal_type(np.VisibleDeprecationWarning()) # E: numpy.VisibleDeprecationWarning
7+
reveal_type(np.ComplexWarning()) # E: numpy.ComplexWarning
8+
reveal_type(np.RankWarning()) # E: numpy.RankWarning
9+
reveal_type(np.TooHardError()) # E: numpy.TooHardError
10+
reveal_type(np.AxisError(1)) # E: numpy.AxisError

0 commit comments

Comments
 (0)