Passed
Push — master ( 9b1fb7...3baa10 )
by Ramon
01:08
created

default_primitive_deserializer()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import inspect
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Bug introduced by
There seems to be a cyclic import (jsons -> jsons.serializers).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
2
import re
3
from datetime import datetime, timezone, timedelta
4
from enum import Enum, EnumMeta
5
from typing import List, Callable
6
from jsons._common_impl import RFC3339_DATETIME_PATTERN, load_impl, \
7
    snakecase, camelcase, pascalcase, lispcase
8
9
10
def default_datetime_deserializer(obj: str, _: datetime, **__) -> datetime:
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
11
    pattern = RFC3339_DATETIME_PATTERN
12
    if '.' in obj:
13
        pattern += '.%f'
14
        # strptime allows a fraction of length 6, so trip the rest (if exists).
15
        regex_pattern = re.compile('(\.[0-9]+)')
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \. was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
16
        frac = regex_pattern.search(obj).group()
17
        obj = obj.replace(frac, frac[0:7])
18
    if obj[-1] == 'Z':
19
        dattim_str = obj[0:-1]
20
        dattim_obj = datetime.strptime(dattim_str, pattern)
21
    else:
22
        dattim_str, offset = obj.split('+')
23
        dattim_obj = datetime.strptime(dattim_str, pattern)
24
        hours, minutes = offset.split(':')
25
        tz = timezone(offset=timedelta(hours=int(hours), minutes=int(minutes)))
0 ignored issues
show
Coding Style Naming introduced by
The name tz does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
26
        datetime_list = [dattim_obj.year, dattim_obj.month, dattim_obj.day,
27
                         dattim_obj.hour, dattim_obj.minute, dattim_obj.second,
28
                         dattim_obj.microsecond, tz]
29
        dattim_obj = datetime(*datetime_list)
30
    return dattim_obj
31
32
33
def default_list_deserializer(obj: List, cls, **kwargs) -> object:
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
34
    cls_ = None
35
    if cls and hasattr(cls, '__args__'):
36
        cls_ = cls.__args__[0]
37
    return [load_impl(x, cls_, **kwargs) for x in obj]
38
39
40
def default_enum_deserializer(obj: Enum, cls: EnumMeta, **__) -> object:
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
41
    return cls[obj]
42
43
44
def default_string_deserializer(obj: str, _: type = None, **kwargs) -> object:
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
45
    try:
46
        return load_impl(obj, datetime, **kwargs)
47
    except:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
48
        return obj
49
50
51
def default_primitive_deserializer(obj: object,
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
52
                                   _: type = None, **__) -> object:
53
    return obj
54
55
56
def default_object_deserializer(obj: dict, cls: type,
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
57
                                key_transformer: Callable[[str], str] = None,
58
                                **kwargs) -> object:
59
    if key_transformer:
60
        obj = {key_transformer(key): obj[key] for key in obj}
61
    signature_parameters = inspect.signature(cls.__init__).parameters
62
    # Loop through the signature of cls: the type we try to deserialize to. For
63
    # every required parameter, we try to get the corresponding value from
64
    # json_obj.
65
    constructor_args = dict()
66
    for signature_key, signature in signature_parameters.items():
67
        if obj and signature_key is not 'self':
0 ignored issues
show
introduced by
Comparison to literal
Loading history...
68
            if signature_key in obj:
69
                cls_ = None
70
                if signature.annotation != inspect._empty:
1 ignored issue
show
Coding Style Best Practice introduced by
It seems like _empty was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
71
                    cls_ = signature.annotation
72
                value = load_impl(obj[signature_key], cls_,
73
                                  key_transformer=key_transformer, **kwargs)
74
                constructor_args[signature_key] = value
75
76
    # The constructor arguments are gathered, create an instance.
77
    instance = cls(**constructor_args)
78
    # Set any remaining attributes on the newly created instance.
79
    remaining_attrs = {attr_name: obj[attr_name] for attr_name in obj
80
                       if attr_name not in constructor_args}
81
    for attr_name in remaining_attrs:
82
        loaded_attr = load_impl(remaining_attrs[attr_name],
83
                                type(remaining_attrs[attr_name]),
84
                                key_transformer=key_transformer, **kwargs)
85
        setattr(instance, attr_name, loaded_attr)
86
    return instance
87
88
89
# The following default key transformers can be used with the
90
# default_object_serializer.
91
KEY_TRANSFORMER_SNAKECASE = snakecase
92
KEY_TRANSFORMER_CAMELCASE = camelcase
93
KEY_TRANSFORMER_PASCALCASE = pascalcase
94
KEY_TRANSFORMER_LISPCASE = lispcase
95