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

jsons.deserializers.default_literal   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B default_literal_deserializer() 0 10 6
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