Passed
Pull Request — master (#180)
by
unknown
01:49
created

default_literal_deserializer()   B

Complexity

Conditions 6

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 8.6666
c 0
b 0
f 0
cc 6
nop 5
1
from typing import Literal, Any, get_args
2
3
from jsons.exceptions import DeserializationError
4
5
def default_literal_deserializer(obj: Any, cls: Literal, *, strictly_equal_literal: bool = False, **kwargs):
6
    for literal_value in get_args(cls):
7
        value_equal = obj == literal_value
8
        type_equal = type(obj) == type(literal_value)
9
        if value_equal and (not strictly_equal_literal or type_equal):
10
            break
11
    else:
12
        err_msg = 'Could not match the object "{}" to the Literal: {}'.format(obj, cls)
13
        raise DeserializationError(err_msg, obj, None)
14
    return literal_value
15