Completed
Push — master ( c813f6...bb27a7 )
by Ramon
13s queued 11s
created

_SubArrayTypeMeta.__repr__()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 4
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
from typing import Any
2
3
import numpy as np
4
from typish import SubscriptableType
5
6
from nptyping.types._nptype import NPType
7
8
9
def is_subarray_type(dtype: np.dtype) -> bool:
10
    """Detect if the type is a subarray."""
11
    return (hasattr(dtype, 'shape')
12
            and isinstance(dtype.shape, tuple)
13
            and len(dtype.shape) != 0)
14
15
16 View Code Duplication
class _SubArrayTypeMeta(SubscriptableType):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
17
    def __hash__(cls) -> int:
18
        return hash((cls.base, cls.shape))
19
20
    def __repr__(cls) -> str:
21
        if cls.base is not None:
22
            return 'SubArrayType[{0}, {1}]'.format(cls.base, cls.shape)
23
        return 'SubArrayType'
24
25
    def __eq__(cls, instance: Any) -> bool:
26
        if hasattr(instance, 'base') and hasattr(instance, 'shape'):
27
            return instance.base == cls.base and instance.shape == cls.shape
28
        return False
29
30
    def __instancecheck__(cls, instance: Any) -> bool:
31
        from nptyping.functions._get_type import get_type
32
33
        if is_subarray_type(instance):
34
            if cls == instance:
35
                return True
36
            return cls == get_type(instance)
37
        return False
38
39
    __str__ = __repr__
40
    __subclasscheck__ = __eq__
41
42
43 View Code Duplication
class SubArrayType(NPType, metaclass=_SubArrayTypeMeta):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
    """
45
    Corresponds to the dtype of a NumPy subarray.
46
    """
47
    base = None
48
    shape = None
49
50
    @classmethod
51
    def _after_subscription(cls, args: Any) -> None:
52
        from nptyping.functions._get_type import get_type
53
54
        if isinstance(args, np.dtype):
55
            cls.base = get_type(args.base)
56
            cls.shape = args.shape
57
        elif isinstance(args, tuple):
58
            cls.base, cls.shape = args
59
        else:
60
            raise Exception(
61
                'Incompatible arguments to SubArrayType: {}'.format(args))
62