8000 Added Warning and Exception annotations by BvB93 · Pull Request #57 · 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.

Added Warning and Exception annotations #57

Merged
merged 3 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
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
Next Next commit
Implemented Warning and Exception annotations
See https://github.com/numpy/numpy-stubs/issues/54.

Added annotations and tests for:
* np.warnings
* np.ModuleDeprecationWarning
* np.VisibleDeprecationWarning
* np.ComplexWarning
* np.RankWarning
* np.TooHardError
* np.AxisError
  • Loading branch information
Bas van Beek committed Apr 18, 2020
commit 2a1c59cd0cddee45a0746e77a28b38e76dd6838d
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: types.ModuleType = np.warnings
ModuleDeprecationWarning: Type[DeprecationWarning] = np.ModuleDeprecationWarning
VisibleDeprecationWarning: Type[UserWarning] = np.VisibleDeprecationWarning
ComplexWarning: Type[RuntimeWarning] = np.ComplexWarning
RankWarning: Type[UserWarning] = np.RankWarning

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

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