• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pynamodb / PynamoDB / 5263446575

pending completion
5263446575

Pull #1079

github

web-flow
Merge 69de228e1 into 12e127f5a
Pull Request #1079: Switch back to botocore _make_api_call under the hood

60 of 60 new or added lines in 7 files covered. (100.0%)

3 existing lines in 2 files now uncovered.

2822 of 2977 relevant lines covered (94.79%)

4.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/pynamodb/_util.py
1
import json
5✔
2
from base64 import b64decode
5✔
3
from base64 import b64encode
5✔
4
from typing import Any
5✔
5
from typing import Dict
5✔
6

7
from pynamodb.constants import BINARY
5✔
8
from pynamodb.constants import BINARY_SET
5✔
9
from pynamodb.constants import BOOLEAN
5✔
10
from pynamodb.constants import LIST
5✔
11
from pynamodb.constants import MAP
5✔
12
from pynamodb.constants import NULL
5✔
13
from pynamodb.constants import NUMBER
5✔
14
from pynamodb.constants import NUMBER_SET
5✔
15
from pynamodb.constants import STRING
5✔
16
from pynamodb.constants import STRING_SET
5✔
17

18

19
def attr_value_to_simple_dict(attribute_value: Dict[str, Any], force: bool) -> Any:
5✔
20
    attr_type, attr_value = next(iter(attribute_value.items()))
5✔
21
    if attr_type == LIST:
5✔
22
        return [attr_value_to_simple_dict(v, force) for v in attr_value]
5✔
23
    if attr_type == MAP:
5✔
24
        return {k: attr_value_to_simple_dict(v, force) for k, v in attr_value.items()}
5✔
25
    if attr_type == NULL:
5✔
26
        return None
5✔
27
    if attr_type == BOOLEAN:
5✔
28
        return attr_value
5✔
29
    if attr_type == STRING:
5✔
30
        return attr_value
5✔
31
    if attr_type == NUMBER:
5✔
32
        return json.loads(attr_value)
5✔
33
    if attr_type == BINARY:
5✔
34
        if force:
5✔
35
            return b64encode(attr_value).decode()
5✔
36
        raise ValueError("Binary attributes are not supported")
5✔
37
    if attr_type == BINARY_SET:
5✔
38
        if force:
5✔
39
            return [b64encode(v).decode() for v in attr_value]
5✔
40
        raise ValueError("Binary set attributes are not supported")
5✔
41
    if attr_type == STRING_SET:
5✔
42
        if force:
5✔
43
            return attr_value
5✔
44
        raise ValueError("String set attributes are not supported")
5✔
45
    if attr_type == NUMBER_SET:
5✔
46
        if force:
5✔
47
            return [json.loads(v) for v in attr_value]
5✔
48
        raise ValueError("Number set attributes are not supported")
5✔
49
    raise ValueError("Unknown attribute type: {}".format(attr_type))
×
50

51

52
def simple_dict_to_attr_value(value: Any) -> Dict[str, Any]:
5✔
53
    if value is None:
5✔
54
        return {NULL: True}
5✔
55
    if value is True or value is False:
5✔
56
        return {BOOLEAN: value}
5✔
57
    if isinstance(value, (int, float)):
5✔
58
        return {NUMBER: json.dumps(value)}
5✔
59
    if isinstance(value, str):
5✔
60
        return {STRING: value}
5✔
61
    if isinstance(value, list):
5✔
62
        return {LIST: [simple_dict_to_attr_value(v) for v in value]}
5✔
63
    if isinstance(value, dict):
5✔
64
        return {MAP: {k: simple_dict_to_attr_value(v) for k, v in value.items()}}
5✔
65
    raise ValueError("Unknown value type: {}".format(type(value).__name__))
×
66

67

68
def _b64encode(b: bytes) -> str:
5✔
69
    return b64encode(b).decode()
5✔
70

71

72
def bin_encode_attr(attr: Dict[str, Any]) -> None:
5✔
73
    if BINARY in attr:
5✔
74
        attr[BINARY] = _b64encode(attr[BINARY])
5✔
75
    elif BINARY_SET in attr:
5✔
76
        attr[BINARY_SET] = [_b64encode(v) for v in attr[BINARY_SET]]
5✔
77
    elif MAP in attr:
5✔
78
        for sub_attr in attr[MAP].values():
5✔
79
            bin_encode_attr(sub_attr)
5✔
80
    elif LIST in attr:
5✔
81
        for sub_attr in attr[LIST]:
5✔
82
            bin_encode_attr(sub_attr)
5✔
83

84

85
def bin_decode_attr(attr: Dict[str, Any]) -> None:
5✔
86
    if BINARY in attr:
5✔
87
        attr[BINARY] = b64decode(attr[BINARY])
5✔
88
    elif BINARY_SET in attr:
5✔
89
        attr[BINARY_SET] = [b64decode(v) for v in attr[BINARY_SET]]
5✔
90
    elif MAP in attr:
5✔
91
        for sub_attr in attr[MAP].values():
5✔
92
            bin_decode_attr(sub_attr)
5✔
93
    elif LIST in attr:
5✔
UNCOV
94
        for sub_attr in attr[LIST]:
×
UNCOV
95
            bin_decode_attr(sub_attr)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc