structured_data._adt.prewritten_methods   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 118
Duplicated Lines 33.9 %

Importance

Changes 0
Metric Value
wmc 32
eloc 85
dl 40
loc 118
rs 9.84
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A PrewrittenProductMethods.__le__() 0 4 2
A CommonPrewrittenMethods.__ne__() 0 4 2
A PrewrittenProductMethods.__ge__() 0 4 2
A PrewrittenProductMethods.__gt__() 0 4 2
A CommonPrewrittenMethods.__repr__() 0 3 1
A PrewrittenProductMethods.__lt__() 0 4 2
A CommonPrewrittenMethods.__eq__() 0 4 2
A CommonPrewrittenMethods.__hash__() 0 2 1
A PrewrittenSumMethods.__init_subclass__() 0 2 1
A PrewrittenSumMethods.__lt__() 10 10 4
A PrewrittenSumMethods.__ge__() 10 10 4
A PrewrittenSumMethods.__gt__() 10 10 4
A PrewrittenSumMethods.__le__() 10 10 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A sum_base() 0 3 1

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
"""Methods to be added to ADT classes."""
2
3
import typing
4
import weakref
5
6
from .._unpack import unpack
7
from .constructor import ADT_BASES
8
9
10
def sum_base(obj: typing.Any) -> typing.Optional[type]:
11
    """Return the immediate base class of the type of a ``Sum`` instance."""
12
    return ADT_BASES.get(obj.__class__)
13
14
15
SUBCLASS_ORDER: typing.MutableMapping[
16
    type, typing.Tuple[type, ...]
17
] = weakref.WeakKeyDictionary()
18
19
20
class CommonPrewrittenMethods(tuple):
21
    """Methods to slot into various modified classes."""
22
23
    def __repr__(self) -> str:
24
        return "{}({})".format(
25
            self.__class__.__qualname__, ", ".join(repr(item) for item in unpack(self))
26
        )
27
28
    def __eq__(self, other: object) -> bool:
29
        if other.__class__ is self.__class__:
30
            return unpack(self) == unpack(typing.cast(tuple, other))
31
        return False
32
33
    def __ne__(self, other: object) -> bool:
34
        if other.__class__ is self.__class__:
35
            return unpack(self) != unpack(typing.cast(tuple, other))
36
        return True
37
38
    def __hash__(self) -> int:
39
        return hash(unpack(self))
40
41
42
class PrewrittenProductMethods(CommonPrewrittenMethods):
43
    """Methods for subclasses of ``structured_data.adt.Product``."""
44
45
    def __lt__(self, other: tuple) -> bool:
46
        if other.__class__ is self.__class__:
47
            return unpack(self) < unpack(other)
48
        raise TypeError
49
50
    def __le__(self, other: tuple) -> bool:
51
        if other.__class__ is self.__class__:
52
            return unpack(self) <= unpack(other)
53
        raise TypeError
54
55
    def __gt__(self, other: tuple) -> bool:
56
        if other.__class__ is self.__class__:
57
            return unpack(self) > unpack(other)
58
        raise TypeError
59
60
    def __ge__(self, other: tuple) -> bool:
61
        if other.__class__ is self.__class__:
62
            return unpack(self) >= unpack(other)
63
        raise TypeError
64
65
66
class PrewrittenSumMethods(CommonPrewrittenMethods):
67
    """Methods for subclasses of ``structured_data.adt.Sum``."""
68
69
    def __init_subclass__(cls, **kwargs: typing.Any) -> None:
70
        raise TypeError(f"Cannot further subclass the class {cls.__name__}")
71
72 View Code Duplication
    def __lt__(self, other: tuple) -> bool:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
        if other.__class__ is self.__class__:
74
            return unpack(self) < unpack(other)
75
        self_base = sum_base(self)
76
        if sum_base(other) is self_base and self_base is not None:
77
            order = SUBCLASS_ORDER[self_base]
78
            self_index = order.index(self.__class__)
79
            other_index = order.index(other.__class__)
80
            return self_index < other_index
81
        raise TypeError
82
83 View Code Duplication
    def __le__(self, other: tuple) -> bool:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
        if other.__class__ is self.__class__:
85
            return unpack(self) <= unpack(other)
86
        self_base = sum_base(self)
87
        if sum_base(other) is self_base and self_base is not None:
88
            order = SUBCLASS_ORDER[self_base]
89
            self_index = order.index(self.__class__)
90
            other_index = order.index(other.__class__)
91
            return self_index <= other_index
92
        raise TypeError
93
94 View Code Duplication
    def __gt__(self, other: tuple) -> bool:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
95
        if other.__class__ is self.__class__:
96
            return unpack(self) > unpack(other)
97
        self_base = sum_base(self)
98
        if sum_base(other) is self_base and self_base is not None:
99
            order = SUBCLASS_ORDER[self_base]
100
            self_index = order.index(self.__class__)
101
            other_index = order.index(other.__class__)
102
            return self_index > other_index
103
        raise TypeError
104
105 View Code Duplication
    def __ge__(self, other: tuple) -> bool:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
        if other.__class__ is self.__class__:
107
            return unpack(self) >= unpack(other)
108
        self_base = sum_base(self)
109
        if sum_base(other) is self_base and self_base is not None:
110
            order = SUBCLASS_ORDER[self_base]
111
            self_index = order.index(self.__class__)
112
            other_index = order.index(other.__class__)
113
            return self_index >= other_index
114
        raise TypeError
115
116
117
__all__ = ["PrewrittenProductMethods", "PrewrittenSumMethods"]
118