nptyping._hashed_subscriptable_type   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 54.55 %

Importance

Changes 0
Metric Value
eloc 12
dl 12
loc 22
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A HashedSubscriptableType.__getitem__() 5 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from typing import Any
2
3
from typish import SubscriptableType
4
5
6 View Code Duplication
class HashedSubscriptableType(SubscriptableType):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    """
8
    An extension of SubscriptableType that caches the __getitem__ to ensure an
9
    identical result per set of parameters.
10
    """
11
    _cache = {}
12
13
    def __getitem__(cls, item: Any) -> Any:
14
        # Take the item's class name into account to distinguish between
15
        # e.g. ints and floats.
16
        hashed = hash('{}{}{}'.format(
17
            cls.__name__, item.__class__.__name__, item))
18
        if hashed not in cls._cache:
19
            cls._cache[hashed] = SubscriptableType.__getitem__(cls, item)
20
        result = cls._cache[hashed]
21
        return result
22