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

Added Warning and Exception annotations #57

Merged
merged 3 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import builtins
import sys
import datetime as dt
import warnings as _warnings

from numpy.core._internal import _ctypes
from typing import (
Expand Down Expand Up @@ -778,3 +779,17 @@ trunc: ufunc

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

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

# Errors
class TooHardError(RuntimeError): ...
class AxisError(ValueError, IndexError):
# Note that all parameters for __init__ (except self) can technically be Any.
# So it's a question here whether or not we should allow what's possible or what's intended
def __init__(self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ...) -> None: ...
18 changes: 18 additions & 0 deletions tests/pass/warnings_and_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import types
from typing import Type

import numpy as np

warnings = np.warnings # type: types.ModuleType
ModuleDeprecationWarning = np.ModuleDeprecationWarning # type: Type[DeprecationWarning]
VisibleDeprecationWarning = np.VisibleDeprecationWarning # type: Type[UserWarning]
ComplexWarning = np.ComplexWarning # type: Type[RuntimeWarning]
RankWarning = np.RankWarning # type: Type[UserWarning]

TooHardError = np.TooHardError # type: Type[RuntimeError]
AxisError1 = np.AxisError # type: Type[ValueError]
AxisError2 = np.AxisError # type: Type[IndexError]

np.AxisError(1)
np.AxisError(1, ndim=2)
np.AxisError(1, ndim=2, msg_prefix='error')