Passed
Pull Request — master (#23)
by Ramon
55s
created

jsons._compatibility_impl.Flag.__or__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
"""
2
PRIVATE MODULE: do not import (from) it directly.
3
4
This module contains functionality for supporting the compatibility of jsons
5
with multiple Python versions.
6
"""
7
from enum import Enum
8
9
10
class Flag(Enum):
11
    """
12
    This is a light version of the Flag enum type that was introduced in
13
    Python3.6. It supports the use of pipes for members (Flag.A | Flag.B).
14
    """
15
16
    @classmethod
17
    def _get_inst(cls, value):
18
        try:
19
            result = cls(value)
20
        except ValueError:
21
            pseudo_member = object.__new__(cls)
22
            pseudo_member._value_ = value
23
            contained = [elem.name for elem in cls if elem in pseudo_member]
24
            pseudo_member._name_ = '|'.join(contained)
25
            result = pseudo_member
26
        return result
27
28
    def __or__(self, other: 'Flag') -> 'Flag':
29
        new_value = other.value | self.value
30
        return self._get_inst(new_value)
31
32
    def __contains__(self, item: 'Flag') -> bool:
33
        return item.value == self.value & item.value
34
35
    __ror__ = __or__
36
37
38
def tuple_with_ellipsis(tup: type) -> bool:
39
    # Python3.5: Tuples have __tuple_use_ellipsis__
40
    # Python3.7: Tuples have __args__
41
    use_el = getattr(tup, '__tuple_use_ellipsis__', None)
42
    if use_el is None:
43
        use_el = tup.__args__[-1] is ...
44
    return use_el
45
46
47
def get_union_params(un: type) -> list:
48
    # Python3.5: Unions have __union_params__
49
    # Python3.7: Unions have __args__
50
    return getattr(un, '__union_params__', getattr(un, '__args__', None))
51