jsons._fork_impl   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 21
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A fork() 0 28 2
1
"""
2
PRIVATE MODULE: do not import (from) it directly.
3
4
This module contains the implementation of ``fork()``.
5
"""
6
from typing import Type, Optional
7
8
from jsons._common_impl import StateHolder, get_class_name, T
9
10
11
def fork(
12
        fork_inst: Type[T] = StateHolder,
13
        name: Optional[str] = None) -> Type[T]:
14
    """
15
    Fork from the given ``StateHolder`` to create a separate "branch" of
16
    serializers and deserializers.
17
    :param fork_inst: The ``StateHolder`` on which the new fork is based.
18
    :param name: The ``__name__`` of the new ``type``.
19
    :return: A "fork inst" that can be used to separately store
20
    (de)serializers from the regular ``StateHolder``.
21
    """
22
    fork_inst._fork_counter += 1
23
    if name:
24
        class_name = name
25
    else:
26
        class_name = '{}_fork{}'.format(
27
            get_class_name(fork_inst),
28
            fork_inst._fork_counter
29
        )
30
    result = type(class_name, (fork_inst,), {})
31
    result._classes_serializers = fork_inst._classes_serializers.copy()
32
    result._classes_deserializers = fork_inst._classes_deserializers.copy()
33
    result._serializers = fork_inst._serializers.copy()
34
    result._deserializers = fork_inst._deserializers.copy()
35
    result._fork_counter = 0
36
    result._suppress_warnings = fork_inst._suppress_warnings
37
    result._suppressed_warnings = fork_inst._suppressed_warnings.copy()
38
    return result
39