Passed
Push — master ( c3c399...6aa0ae )
by Ramon
55s queued 10s
created

typish.classes._literal   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 66.67 %

Importance

Changes 0
Metric Value
eloc 30
dl 46
loc 69
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _LiteralMeta.__subclasscheck__() 2 2 1
A LiteralAlias.from_literal() 11 11 2
A _LiteralMeta.__instancecheck__() 2 2 1
A _LiteralMeta.__str__() 2 2 1
A _LiteralMeta.__getattribute__() 18 18 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A is_literal_type() 0 9 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
import typing
2
3
from typish.classes._subscriptable_type import SubscriptableType
4
5
6
def is_literal_type(cls: typing.Any) -> bool:
7
    """
8
    Return whether cls is a Literal type.
9
    :param cls: the type that is to be checked.
10
    :return: True if cls is a Literal type.
11
    """
12
    from typish.functions._get_simple_name import get_simple_name
13
14
    return get_simple_name(cls) == 'Literal'
15
16
17 View Code Duplication
class _LiteralMeta(SubscriptableType):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
    """
19
    A Metaclass that exists to serve Literal and alter the __args__ attribute.
20
    """
21
    def __getattribute__(cls, item):
22
        """
23
        This method makes sure that __args__ is a tuple, like with
24
        typing.Literal.
25
        :param item: the name of the attribute that is obtained.
26
        :return: the attribute.
27
        """
28
        if item == '__args__':
29
            try:
30
                result = SubscriptableType.__getattribute__(cls, item),
31
            except AttributeError:
32
                # In case of Python 3.5
33
                result = tuple()
34
        elif item in ('__origin__', '__name__', '_name'):
35
            result = 'Literal'
36
        else:
37
            result = SubscriptableType.__getattribute__(cls, item)
38
        return result
39
40
    def __instancecheck__(self, instance):
41
        return self.__args__ and self.__args__[0] == instance
42
43
    def __str__(self):
44
        return '{}[{}]'.format(self.__name__, self.__args__[0])
45
46
    def __subclasscheck__(self, subclass: typing.Any) -> bool:
47
        return is_literal_type(subclass)
48
49
50 View Code Duplication
class LiteralAlias(type, metaclass=_LiteralMeta):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
    """
52
    This is a backwards compatible variant of typing.Literal (Python 3.8+).
53
    """
54
    @staticmethod
55
    def from_literal(literal: typing.Any) -> typing.Type['LiteralAlias']:
56
        """
57
        Create a LiteralAlias from the given typing.Literal.
58
        :param literal: the typing.Literal type.
59
        :return: a LiteralAlias type.
60
        """
61
        from typish.functions._get_args import get_args
62
63
        args = get_args(literal)
64
        return LiteralAlias[args[0]] if args else LiteralAlias
65
66
67
# If Literal is available (Python 3.8+), then return that type instead.
68
Literal = getattr(typing, 'Literal', LiteralAlias)
69