Passed
Pull Request — master (#15)
by Ramon
03:38
created

nptyping.ndarray   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 8
dl 0
loc 55
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A NDArray.type_of() 0 12 1
1
from typing import Type
2
3
import numpy as np
4
5
from nptyping._ndarray_meta import _NDArray
6
7
8
class NDArray(np.ndarray, _NDArray):
9
    """
10
    NDArray is a representation of numpy.ndarray.
11
12
    An Array with any dimensions of any size and any type:
13
    >>> NDArray
14
    NDArray[(typing.Any, ...), typing.Any]
15
16
    An array with 1 dimension of any size and any type:
17
    >>> from typing import Any
18
    >>> NDArray[Any]
19
    NDArray[(typing.Any,), typing.Any]
20
21
    An array with 1 dimension of size 3 and any type:
22
    >>> NDArray[3]
23
    NDArray[(3,), typing.Any]
24
25
    An array with any dimensions of any size and type int:
26
    >>> NDArray[int]
27
    NDArray[(typing.Any, ...), int]
28
29
    An array with 1 dimension of size 3 and type int:
30
    >>> NDArray[3, int]
31
    NDArray[(3,), int]
32
33
    An array with any dimensions of size 3 and type int:
34
    >>> NDArray[(3, ...), int]
35
    NDArray[(3, ...), int]
36
37
    An array with 3 dimensions of sizes 3, 3, 5 and type int:
38
    >>> NDArray[(3, 3, 5), int]
39
    NDArray[(3, 3, 5), int]
40
41
    """
42
43
    @staticmethod
44
    def type_of(arr: np.ndarray) -> Type['NDArray']:
45
        """
46
        Return an nptyping.NDArray type T such that isinstance(arr, T).
47
48
        >>> NDArray.type_of(np.array([[1, 2], [3, 4.0]]))
49
        NDArray[(2, 2), float64]
50
51
        :param arr: any numpy.ndarray.
52
        :return: a nptyping.NDArray.
53
        """
54
        return NDArray[arr.shape, arr.dtype]
55